I want to check that text is now not present on a webpage without my test failing
I have achieved this by catching the exception but is there a better way of doing this?
try {
selenium.isTextPresent(selenium.getText("27"));
} catch (Exception e) {
System.out.println("Element does not exhist");
}
There's nothing wrong with try-catch.
Anyway, what you have should be more of a isElementPresent("27") since you discard the return value of isTextPresent(). Moreover, if the element exists, then isTextPresent() will always return true, because ... well, you took the text out of existing element, it has to be there. In this case, it is enough just to assure whether the element exists or not.
But if you do actually need it in real code somehow, then what about if (selenium.isElementPresent("27") && selenium.isTextPresent(selenium.getText("27"))) ?
Also, getXpathCount(//*[#id='27' and text()]) == 0 expression does the trick, too. It count the number of returned elements that have id=27 and contain some text. If that's a zero, there are none.
you can use assertTextNotPresent OR verifyTextNotPresent
# Slanec Yes, its my mistake
You can use
verifyFalse(selenium.isTextPresent("text"));
assertFalse(selenium.isTextPresent("text"));
Based on the docs you should be able to say the following:
selenium.isTextPresent("27"));
This returns a boolean so if you want to make sure it is not there... check it is false.
Related
I am relatively new to development so please forgive me if some of this seems rather amateurish. Part of my reason for posting the question is to help nudge me to the answer, part is to make sure I'm following good coding practice.
The challenge -
I'm using Java & Selenium to check a very large, dynamically populated table. I need to find a specific list of elements where the text matches a case-sensitive String -
List<WebElement> AllPaths = getCurrentDriver().findElements(By.xpath("//*[text()[contains(.,'" + fixedString + "')]]"));
The table I'm checking is basically a large calendar-style grid. If I don't find evidence of fixedString, I then want to iterate back one month at a time until I DO find the fixedString.
The problem -
The code above returns an exception if it cannot find an element. My first thought was to setup a while loop, trying/catching the exception and then repeating until exceptions stopped. However this feels wrong to me - I don't think I should be essentially "swallowing" exceptions. That said, I'm not sure what the correct way of trying to find this element is that doesn't lead to an exception if it fails to locate it.
Am I right in thinking it's a bad idea to write code that you know causes an exception and then simply swallow it and move on?
Hope this makes sense, as I say I'm a beginner so please be gentle :)
You can try something like -
if(AllPaths.size()>0){
//logic when elements found with fixed string
}else{
//logic to iterate over another month
}
Also, your statement seems wrong to me. It should be -
List<WebElement> AllPaths = getCurrentDriver().findElements(By.xpath("//*[contains(text(),'" + fixedString + "')]"));
findElements doesn't throw exception this way. It will return empty if no elements located. The exception seems due to incorrect statement that you are using to find elements.
The code above returns an exception if it cannot find an element.
The documentation says findElements returns an empty list when no elements are found. It should not throw any exception in this case. Is it possible that you by mistake used findElement instead of findElements? What type of exception is thrown and what is the message?
You shouldn't need to catch exceptions here. You are right though that control flow using exceptions should be avoided and that swallowing exceptions is bad. On the other hand frameworks don't always let you write code the way you want so sometimes exceptions have to be made.
In this condition I want to read data from a file, but not all the words. Is this condition correct? set in the following code is a HashSet.
if (!set.contains(word.toString().equals(set)))
{
word.set(str);
context.write(word, one);
}
else
continue;
This code would not work, because the contains method on Set checks by means of equals, whether the Set contains the item. You do not need to use the equals check again.
if (!set.contains(word))
{
word.add(word);
}
else
continue;
I am not sure I get what you asked, but hope this helps. Leave a comment and I would be glad to edit/remove my answer.
I am currently writing a test script, in java on selenium, which is looking at elements on the webpage and if present writing them into a text file.
The issue I have is that the element may or may not be on the page, depending on other variables.
I am trying to get the code to set whether the element is present as a boolean, and if present complete execute the 'if block', if the element is not present I want it to go to 'else' and continue with the script.
Here's my current effort, however during execution (when the element is not present) the script is ending in exception, in the if block (because the element is not present), when I don't expect it to enter in the first place.
// First check if the element exists (attempting to make boolean)
boolean isPresent; Login.driver.findElements
(By.xpath("html/body/div[4]/div[1]/table[1]/tbody/tr[3]/td[2]"));
// If true write values to .txt file, else continue with script
if (true){
writer.write(Login.driver.findElement
(By.xpath("html/body/div[4]/div[1]/table[1]/tbody/tr[1]/td[2]/a"))
.getText() + " : " + Login.driver.findElement
(By.xpath("html/body/div[4]/div[1]/table[1]/tbody/tr[3]/td[2]"))
.getText());
writer.write(lineSeparator);}
else
Can anyone see where I have gone wrong?
OK first of all I'm assuming you are using Selenium WebDriver and not Selenium RC.
You need to assign the boolean to true if the elements exist.
Assuming that:
Login.driver.findElements(
By.xpath("html/body/div[4]/div[1]/table[1]/tbody/tr[3]/td[2]"));
Can find the elements (I don't personally recommend traversing the DOM tree), then you just need to check if it did and report that to the isPresent boolean.
Since findElements returns a List you need to check if its size is greater than 0.
And you assign the result to the boolean like so:
boolean isPresent = (Login.driver.findElements(
By.xpath("html/body/div[4]/div[1]/table[1]/tbody/tr[3]/td[2]")).size() > 0);
Then just put your boolean in the if conditional if(isPresent) and it will only execute when the elements are found.
Supposed I have this kind of loop in java (not an exact code, neither it's a truly valid code, but just to give you a general perspective of situation):
print "<ul>";
while (res = fetch(database)) {
print ("<li>" + res.col['data'] + "</li>");
}
print "</ul>";
and I have this CSS, which makes the last item on the list has red color.
ul li:last-child { color: red; }
this works fine on most browsers. the problems are:
I need to make this work on IE8 too.
IE8 doesn't support last-child.
I cannot test whether current iteration of "while" is entering last iteration or not. or let's say, there's no way I could check when the loop would end. this is not an ordinary loop, but let's just say like that. so I can't give the last <li> a class, say class="lastchild".
I also tried with javascript and jquery, and both can't select "last-child" either, as they depends on the css for the selection, I think.
how is the best approach in this situation? thanks.
print "<ul>";
String liText=null;
while (res = fetch(database)) {
if (liText!=null) {
print ("<li>" + liText + "</li>");
}
liText=res.col['data'];
}
if (liText!=null) {
print ("<li style='the last element style'>" + liText + "</li>");
}
print "</ul>";
Move the adding of the last line out of the while and apply desired style there
Assuming that the problem is in Java, I am wondering why there is no way to detect last record. Probably you need to reconsider you data structure first, populate the data in a Map or a List and then iterate over.
And, if that is not possible, you can possibly opt for a StringBuilder instnace to build the entire String html tags first by looping over and then replace the last li element with the desired style and after that put it in the print statement for rendering.
The above points are valid even if you are trying to code in javascript, with little bit of change.
Inside Fitnesse DoFixture, you can use check keyword to compare one of the objects with an expected value. I was wondering if there is a check not equal that exists to make sure the expected and actual do not match up.
I have tried using that set of keywords but it is not supported. There is a reject keyword for DoFixtures, but it does not accomplish the goal. Anyone know of a method? For a testing framework, it seems like it should be obvious, but I've had a rough time digging through the UserGuide.
Example:
|check not equal| guid | c1acff01-e45b-4b7d-b6f5-84f8830ef6b4 |
Scenario Pass: guid != c1acff01-e45b-4b7d-b6f5-84f8830ef6b4
I was unable to find this type of test condition, so instead, I put the logic inside the Fixture code with the expectation to return true when a != b. In the Fit code, I had the following:
|check|guidsNotEqual|true|
With fitSharp you can have
|check|mymethod|fail[myvalue]|
This will pass if mymethod is not equal to myvalue. I don't think this works with the Java FitLibrary.
There is an option (might be added after the question was asked). It is possible to check for something not equal by using 'check not'.
So
|check not|this is true|false|
will return true