Selenium WebDriver if else statement - java

first time poster, long time user reaping the benefits of all these great questions. But I need your help.
What I'm trying to do below is
Navigate to a page
Find all the particular links
Click on the first link
Check to see if an element is displayed, if it is displayed then navigate back to the previous page and click on the next link of the list. If it is NOT displayed then exit out of the method and continue the test script. This is the part where I'm stuck.
The if statement executes as desired whereby if it finds the element then it navigates back to the previous. But where it fails is when it clicks on the second link of the page. It searches for that element even though that element does not exist in that page and does not exit out of the method even though I've explicitly stated return.
I'm having a brain fart and tried all the possible combinations and permuatations I can think of. If there's anyone out there that can help me I'd greatly appreciate the help.
EDIT
Let me edit to clarify my thoughts. I need my method to exit out of the method once inactive.isDisplayed() returns false. But when it navigates to the second page, it continually tries to find the element then eventually fails with a NoSuchElementException. I know the element doesn't exist, that's why I need it to exit out of the method and perform the next step of the test script. I hope this clarify my situation. It's not really a Selenium WebDriver question as it is a java question.
Thanks
public void checkErrors() {
List<WebElement> videos =driver.findElements(By.cssSelector(".row-
title"));
for (int i = 0; i < videos.size(); i++) {
videos = driver.findElements(By.cssSelector(".row-title"));
videos.get(i).click();
if (inactive().isDisplayed() != false) {
driver.navigate().back();
} else {
return;
}
}
return;
}
EDIT:
private WebElement inactive() {
inactive =
driver.findElement(By.cssSelector("#message>p>strong"));
highlightElement(inactive);
return inactive;
}

You might want to check the presence of the message before checking if it's displayed:
public void checkErrors() {
for(int i = 0; ; i++) {
// handle next link
List<WebElement> videos = driver.findElements(By.cssSelector(".row-title"));
if (i >= videos.size())
return;
// click the next link
WebElement video = videos.get(i);
video.click();
// return if the message is missing or hidden
List<WebElement> messages = driver.findElements(By.cssSelector("#message>p>strong"));
if (messages.size() == 0 || !messages.get(0).isDisplayed())
return;
driver.navigate().back();
}
}

Small recommendation to help you here:
As you're not using the WebElement returned by inactive() after you've checked if it's displayed or not, you might as well move the logic for checking whether its displayed to inactive() and return the value of isDisplayed(). For example:
private boolean inactive() {
try {
inactive = driver.findElement(By.cssSelector("#message>p>strong"));
highlightElement(inactive);
return inactive.isDisplayed(); // Element is present in the DOM but may not visible due to CSS etc.
} catch (NoSuchElementException e) {
return false; // Element is not present in the DOM, therefore can't be visible.
}
}

Related

Delete a record from web table scenario in Selenium Webdriver

I am writing an automation script and one of the scenarios I wish to automate is to delete a record from grid, now what I am doing is finding the xpath of the list of delete buttons in the grid, and I'm hitting an 'if' condition where I state that if the delete button is displayed on page delete the first record or else driver.close(); but I guess Selenium isn't checking the condition, it is directly showing me NoSuchElement exception. Can someone please suggest me a better way or some other alternative to automate such a scenario. The code I'm using:=
//resourceSchedulePage - Class object
//clickDeleteResourceScheduleDataBtn() - method that returns the WebElement
schedulerPage.clickResourceSchedule().click();
logger.info("Resource schedule link is clicked");
Thread.sleep(500);
if(resourceSchedulePage.clickDeleteResourceScheduleDataBtn().isDisplayed())
{
resourceSchedulePage.clickDeleteResourceScheduleDataBtn().click();
Thread.sleep(500);
}
else
{
driver.close();
}
Implementation :
#FindBy(xpath="//*[#id=\"gridResourceSchedule\"]//td[6]/a[2]")
WebElement deleteResourceScheduleBtn;
public WebElement clickDeleteResourceScheduleDataBtn() throws InterruptedException {
synchronized (driver) {
driver.wait(1000); }
return deleteResourceScheduleBtn; }
As explained by #pburgr, here is the implementation for findElements
if (driver.findElements(By.xpath("Delete button xpath")).size() > 0 ) {
System.out.println("Delete button is avilable");
// you can click on delete here, or whatever you wanna do.
}
else {
System.out.println("Delete button isn't avilable");
driver.close();
}
also there is a way to handle this situation which is try catch block.
element.isDisplayed() can be used for existing element only. It returns false only if the element is found but is not displayed (hidden=true f.e.).
To check if element exists you can use List<WebElement> elements = driver.findElements(...); In case no element is found, you'll get empty List instead of NoSuchElementException.

Selenium, ensure that all the checkbox are checked

I'm using Selenium to test action on a html page. The one i'm working on contains multiple list of check box. My selenium script do the following actions :
-Click on the dropdown list to display the check box list
-Click on all the check box
-Click on the dropdown list to close the check box list
-Repeat for the next list
The problem is that somtimes the script goes too fast for the browser and some box are not checked, it happens really often since i make multiple test
To resolve my problem i tried to make a method that check if the
checkbox is checked, if not then i click again but it's not working and it increases the time of my test. Here my code so far :
public void clickClearanceListBox(int numberInList) throws InterruptedException {
int iteration = countTheNumberOfElement("//div[5]/div["+numberInList+"]/div[2]");
for(int i = 1; i <= iteration; i++) {
String xpathBox ="//div["+numberInList+"]/div[2]/div["+i+"]/div/div/label/span/span[2]";//xpath de la checkbox
String xpathInput = "//div["+numberInList+"]/div[2]/div["+i+"]/div/div/label/input";
clickTheDOMbyJs(xpathBox);
while(!checkBoxChecked(xpathInput)) {
Thread.sleep(200);
clickTheDOMbyJs(xpathBox);
}
}
}
Or :
public void clickClearanceListBox(int numberInList) throws InterruptedException {
int iteration = countTheNumberOfElement("//div[5]/div["+numberInList+"]/div[2]");
for(int i = 1; i <= iteration; i++) {
String xpathBox ="//div["+numberInList+"]/div[2]/div["+i+"]/div/div/label/span/span[2]";//xpath de la checkbox
String xpathInput = "//div["+numberInList+"]/div[2]/div["+i+"]/div/div/label/input";
clickTheDOMbyJs(xpathBox);
while(!checkBoxChecked(xpathInput)) {
Thread.sleep(200);
}
}
}
It looks like you need to use WebDriverWait in here. First, wait for the dropdown list to load completely, then wait for the checkbox to be visible.
A word of warning to you though. These xpaths are likely to be very brittle, and your tests will break quite easily. You should consider finding these various elements using ids, classes, or other attributes.
I manage to do it by checking if the attribute "value" of my checkbox has the value "true". While the attribute value is false, it means that the checkbox is still not checked for the browser so i wait... This extremly slow but at least it's working everytime. After the script clicked on the checkbox i do this :
while(!getAnElementAttribute(xpathInput, "value").equals("true")) {
}

Selenium: StaleElementReferenceException

I've been researching this error for a while and have tried many things and nothing seems to work...
while(!driver.findElements(By.className("next")).isEmpty()) {
//elements = driver.findElements(By.xpath("//a[#class='name']"));
elements = findDynamicElements("//a[#class='name']");
for (WebElement e : elements) {
userName = e.getText(); //<--EXCEPTION HERE
check_visitor_profile(userName);//<--WE LEAVE THE PAGE HERE
Thread.sleep(3000); //<--NO TRY/CATCH BLOCK FOR READABILITY
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
elements = findDynamicElements("//a[#class='name']");
}
driver.findElement(By.xpath("VisitsNext")).click();
}
protected List<WebElement> findDynamicElements(String path) {
List<WebElement> result;
String xPath = path;
new WebDriverWait(driver, 25).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(xPath)));
//new WebDriverWait(driver, 25).until(elementIdentified(By.id(path)));
try {
result = driver.findElements(By.xpath(xPath));
return result;
}
catch(WebDriverException e) {
return null;
}
);
}
My code craps out on the first line of the for loop where userName is assigned. I've seen on this forum that you should use 'presenceOfElementLocated' and explicitly wait for the element to come back but that doesn't work either. I used 'presenceOfAllElementsLocatedBy' for a list but I have a method that uses 'presenceOfElementLocated' which doesn't work either.
I know stuff like the Thread.sleep and the implicitlyWait line is probably unnecessary at this point but I've literally tried everything and it doesn't work...
The error occurs because when I call 'check_visitor_profile' it leaves the page - when it comes back the elements are out of place so I have to find them again. Which I do but it still throws the exception.
Any Ideas?
Thanks.
The problem might occur because you are changing elements in the middle of the loop. It will cause you trouble even without the StaleElementReferenceException. Use a for loop instead of the for each loop
elements = findDynamicElements("//a[#class='name']");
int size = elements.size();
for (int i = 0 ; i < size ; ++i) {
elements = findDynamicElements("//a[#class='name']");
userName = elements.get(i).getText();
check_visitor_profile(userName);
}
Handle the exception explicitly as the element is no longer attached to the DOM or has changed at that moment you call "check_visitor_profile"
See the link below might help
catch(StateElementException e){
System.out.println("StaleElement dealt with since you successfully left page ");
}
http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp

Selenium exception handling, problems

I have problems with Java Selenium. I use it to automate testing web page, which structure is very complicated - a lot of elements are loaded dynamically, there is a lot of unnecessary elements in html pages. It's very difficult to make my tests reliable. Sometimes page can't load or I try to click on the button which doesn't exist yet (in similiar method of course).
So, I wrote Util class with methods like this one:
public static void findAndSendKeys(String vToSet, By vLocator) {
log.info("findAndSendKeys " + vLocator.toString());
int attempts = 0;
while (attempts < ATTEMPTS) {
WebElement element = null;
try {
element = webDriverWait.until(ExpectedConditions.presenceOfElementLocated(vLocator));
element.sendKeys(vToSet);
break;
} catch (TimeoutException e) {
log.error("timeOut exception " + e.getMessage());
} catch (StaleElementReferenceException e) {
log.error("StaleElementReference exception " + e.getMessage());
} catch (UnhandledAlertException e) {
log.error("UnhandledAlert exception " + e.getMessage());
Alert alert = driver.switchTo().alert();
alert.accept();
}
attempts++;
}
}
I know it looks terrible, I didn't refactor it yet, but method usually works fine for most cases - on the second or third loop input field is filled.
Firstly I was using only sendKeys with exception handling, but I noticed that although input field exists, StaleElementReferenceException is thrown, so I put while() in this static method and try to sendKeys again.
Sometimes my webPage shows Alert that is just validation and after catching exception and ignoring alert I can continue work.
I wonder.. It could be easier if there would exist method similiar to "Pause 1000" method in Selenium IDE. Sometimes my web page works fast and good, sometimes page loading process is very long and I have to wait.
There is also problems with while() loop. I don't know what to do if while loop ends and nothing is send - for example loaded page/container is blank, dynamic loading fails, so there is no chance to find our input field
Automate testing process for this web page causes me a headache. Please, be placable, I don't have technical support and I am on my own.
In my opinion, you are trying to overengineer the whole thing. I think you are trying to write a single function that will handle all cases and I don't think that is a good approach. Each case is potentially different and you will need to understand each case and handle it appropriately.
A few tips.
Don't loop a wait.until(). Instead of
for (int i = 0; i < 5; i++)
{
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(...);
}
do
WebDriverWait wait = new WebDriverWait(driver, 50);
wait.until(...);
The end result is the same (max 50s wait) but the 2nd code is less confusing. Read up on waits to better understand them. The default behavior for a wait is to wait up to the max time and poll the condition every 500ms.
If you are getting StaleElementReferenceException, you are missing something. I don't mean that to sound critical... it happens. You need to understand how the HTML of the page is changing because you are looking for an element that is disappearing and then you try to scrape it. You will need to change either the element you are looking for or when you are looking for it.
Don't use Thread.sleep(). In general it's a bad practice because it's hard coded. Depending on the environment, it may be too long or not long enough of a wait. Instead prefer the wait.until() mechanism you are already using. The hard part, at times, is finding what to wait for.
If you are trying to scrape the page after the items are sorted, then you need to determine what changes after sorting occurs. Maybe there's an arrow on the column header that appears to indicate a sort direction (ascending vs descending)? One thing you could do is to grab one of the TD elements, wait for it to go stale (while the elements are being sorted), and then regrab all elements?
Maybe something like this
WebDriverWait wait = new WebDriverWait(driver, 10);
// grab the first TD as a stale reference
WebElement td = driver.findElement(By.tagName("td"));
// wait for it to go stale
wait.until(ExpectedConditions.stalenessOf(td));
// now grab them all and do something with them
List<WebElement> tds = driver.findElements(By.tagName("td"));
Without a link to the page or a lot more explanation of what is happening on the page and some relevant HTML, I'm not sure what else we can provide.
This code works for me:
jsPageLoadWait ="
try {
if (document.readyState !== 'complete') {
return false; // Page not loaded yet
}
if (window.jQuery) {
if (window.jQuery.active) {
return false;
} else if (window.jQuery.ajax && window.jQuery.ajax.active) {
return false;
} else if ($(':animated').length != 0) {
return false;
}
}
if (window.angular) {
if (!window.qa) {
// Used to track the render cycle finish after loading is complete
window.qa = {
doneRendering: false
};
}
// Get the angular injector for this app (change element if necessary)
var injector = window.angular.element('body').injector();
// Store providers to use for these checks
var $rootScope = injector.get('$rootScope');
var $http = injector.get('$http');
var $timeout = injector.get('$timeout');
// Check if digest
if ($rootScope.$$phase === '$apply' || $rootScope.$$phase === '$digest' || $http.pendingRequests.length !== 0) {
window.qa.doneRendering = false;
return false; // Angular digesting or loading data
}
if (!window.qa.doneRendering) {
// Set timeout to mark angular rendering as finished
$timeout(function() {
window.qa.doneRendering = true;
}, 0);
return false;
}
}
return true;
} catch (ex) {
return false;
}"
public static Boolean WaitLoad(this ISearchContext context, UInt32 timeoutInMilliseconds = 10000, UInt32 millisecondPolling = 1000)
{
Boolean waitReadyStateComplete;
var wait = new DefaultWait<ISearchContext>(context);
wait.Timeout = TimeSpan.FromMilliseconds(timeoutInMilliseconds);
wait.PollingInterval = TimeSpan.FromMilliseconds(millisecondPolling);
wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(StaleElementReferenceException));
waitReadyStateComplete = wait.Until<Boolean>(ctx =>
{
if ((Boolean)((IJavaScriptExecutor)context).ExecuteScript(jsPageLoadWait))
return true;
else
return false;
});
return waitReadyStateComplete;
}

Which is the correct way to check if an element is present or displayed on a page with Selenium WebDriver using Java?

I'm testing a web application using Selenium WebDriver and I was wondering which is the proper method to check if the elements are present or displayed. I usually assert that all elements are present on the page but it also checks the hidden elements which in this case would also be necessary to check if the elements are displayed only when some action is done. For example I click a link and other fields and labels are displayed, while they were hidden before. In this case I should both check if the elements are present and also if they are or not displayed before and after some other element is clicked.
I was wondering which is the proper way to do this. Is it too much to check all the elements on the page ( assuming that I have some buttons, text-fields, labels, links etc. in the page)?
For the purpose of discussion I want to include some code snippets. To check that elements are present on the page I use the following snippet:
public boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
To check if an element is displayed I use the following snippet:
public boolean isElementDisplay(By by) {
if (driver.findElement(by).isDisplayed()) {
return true;
} else
return false;
}
One purpose of testing this application is to check if the elements exists and are displayed correctly. Am I doing the right thing here? Please give me your point of view. I've recently started working with Selenium WebDriver and being the only one in my company who does this...I don't have anyone to turn to. Any answer is appreciated.
There are no problems with it, except if you call your "checkIfDisplayed" method on an element that doesn't exist in the first place, it will throw an exception. I would modify it to this:
public boolean checkIfDisplayed(By by) {
if (isElementPresent(by) {
if (driver.findElement(by).isDisplayed()) {
return true;
} else
return false;
} else
return false;
}
(This may not be code that compiles, I am a C# man, but you should see what I mean)
It may have a slight performance hit, but overall what you are doing is perfectly fine anyway.
To check for element's existence I'd rather use
public boolean isElementPresent(By by)
{
return driver.findElements(by).size() > 0
}
I don't really get the idea behind your checkIfDisplayed function. It returns the result returned by WebElemet's isDisplayed() method without adding any new functionality...
EDIT
So far Arran provided the best answer. Just to modify it a little bit:
public boolean checkIfDisplayed(By by)
{
List<WebElemet> elements = driver.findElements(by);
return ((elements.size() > 0) && (elements[0].isDisplayed()));
}
I believe however that it would be better to call isElementPresent and isDisplayed separately. In this way you will know why the test failed (if it was caused by element's existence or visibility)
I use the same approach (I mean the same methods). But it is important to understand two things.
Some element can be present but not visible.
So for the purpose to verify whether element is present we can call the method
public boolean isElementPresent(By locatorKey) {
try {
driver.findElement(locatorKey);
return true;
} catch (NoSuchElementException e) {
return false;
}
But the drawback of isElementPresent method it can point out elements in DOM model that are not visible on the page and consequently are not accesssible with webDriver. So in that case additional check helps us:
driver.findElement(By.xpath(....)).isDisplayed()
Hope things come clear now)
I'd say your first method looks just fine.
The 2nd thus will give you trouble if the findElement-call will give you no result.
You should add a check if there is an element found and then check if it's displayed:
try {
final WebElement elem = driver.findElement(by);
elem.isDisplayed();
return true;
} catch (NoSuchElementException nse) {
return false;
}
Also note: You are ignore the possibility that more than one element matches the criteria given by the by instance. See the findElemts() method.
You can also combine both the methods as per your requirement as follows,
if(driver.findElements(By.LOCATOR).size()>0)
{
if(driver.findElement(By.LOCATOR).isDisplayed())
{ print "Element is present and displayed"; }
else
{ print "Element is present but not displayed"; }
}
else
{ print "Element is not present"; }

Categories