Selenium, ensure that all the checkbox are checked - java

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")) {
}

Related

How to click on a button until an element appears on a page in selenium

I have a page with a dynamic table that periodically updates with new data. What I am trying to do is to click on a button and reload the page let's say every 3 seconds until the element from that table appears.
I know the xpath of the element I want to appear, but I just can't make it work using the FluentWait. I tried configuring as seen in the code below, but when I'm calling the method it keeps clicking on the button extremly fast disregarding the pollingEvery configuration and without giving the page enough time to fully reload itself. Code can be checked out below.
What I am not sure about is the return statement. I don't fully grasp what should it be if I only need to click on a button until that element appears.
What am I missing?
public void clickButtonUntilElementIsDisplayed(WebElement elementToBeClicked, String xPathOfElementToBeDisplayed){
FluentWait<WebDriver> wait = new FluentWait<>(getDriver());
wait.pollingEvery(Duration.ofSeconds(5));
wait.withTimeout(Duration.ofSeconds(200));
wait.ignoring(NoSuchElementException.class);
Function<WebDriver,WebElement> clickUntilAppearance = webDriver -> {
while(webDriver.findElements(By.xpath(xPathOfElementToBeDisplayed)).isEmpty())
click(elementToBeClicked);
return webDriver.findElement(By.xpath(xPathOfElementToBeDisplayed));
};
wait.until(clickUntilAppearance);
}
The class in which this method can be found extends Page
The return type of the Functional Interface need to be changed. There are 2 cases in which the functional interface will keep looping.
Value returned is null.
It threw an Exception mentioned as part of ignoring
Don't use while. Use if condition. For every pollingEvery this Functional Interface would get executed.
Function<WebDriver,WebElement> clickUntilAppearance = webDriver -> {
List<WebElement> tableElements = webDriver.findElements(By.xpath(xPathOfElementToBeDisplayed));
if(tableElements.isEmpty()) {
click(elementToBeClicked);
return null;
} else {
return tableElements.get(0);
}
};
WebDriverWait w = new WebDriverWait(driver, 5);// 5seconds
w.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("element_location")));

Stale Element Exception Java Selenium

I am trying to crawl trip advisor reviews. Some reviews have the more link, which displays the entire text of the review when clicked. However, when I imitate the click using Selenium, the DOM changes, which gives me the Stale Element Exception. I am trying to work around it by getting the DOM again, but the error still exists. Where am I going wrong?
List reviews = driver.findElements(By.className("review"));
for (int x = 0; x < reviews.size(); x++) {
WebElement element = driver.findElements(By.className("review")).get(x);
try {
if (element.findElements(By.className("expandLink")).size() > 0)
if (element.findElement(By.className("expandLink")).findElement(By.className("ulBlueLinks")).getText().startsWith("More")) {
element.findElement(By.className("ulBlueLinks")).click();
if (driver.findElements(By.className("ui_overlay")).size() > 0)
driver.findElement(By.className("ui_overlay")).findElement(By.className("ui_close_x")).click();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
while (driver.findElements(By.className("ui_close_x")).size() > 0)
driver.findElement(By.className("ui_close_x")).click();
element = driver.findElements(By.className("review")).get(x);
String rating = element.findElement(By.className("ui_bubble_rating")).getAttribute("class").split(" ")[1].replaceAll("bubble_", "");
String review = element.findElement(By.className("entry")).getAttribute("innerHTML").replaceAll("<[^>]*>", "").replaceAll("[,\n]", " ");
I faced same thing, this is due to sometimes problems with DOM elements which are not reachable by selenium causing exception.
I come with this solution and it worked for me.
http://darrellgrainger.blogspot.in/2012/06/staleelementexception.html
Have you considered using the #FindBy annotation for declaring WebElements? Using this annotation, each time you make a call to a WebElement the driver searches the DOM for that WebElement.
This methodology means you only need to declare each element once and a reference to the WebElement is stored. It also allows you to make use of the Page Object Pattern, which will give you clean separation between page and test logic.
Here is a write up on it:
https://www.toptal.com/selenium/test-automation-in-selenium-using-page-object-model-and-page-factory

Selenium WebDriver if else statement

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.
}
}

Finding and Clicking element in list - Selenium

I have been messing around with automating this options page, and since it provides a variety of options each with its own sub-options, I do not want to take the time to identify and declare all of the elements by their xpath(or CSS.. either or). So I have this reliable code that does a good job at finding the text identifier in the HTML tags.
public void selectAnOption(String option) {
List<WebElement> choice = driver.findElements(By.xpath("//div[#class='atcui-scrollPanel-wrapper']/ul/li"));
for(WebElement e : choice){
System.out.println(e.getText());
if(e.getText().equals(option)){
e.click();
break;
}
}
}
By running this I get a printout like
Mileage
Transmission
Gas Type
And so on.So boom! I know that they are identified, but my e.click() is not actually clicking. I get no errors when I start the test it just says it passed but the button was never actually clicked. Below is the HTML segment I am working with and you can see how nested it is.
For Java 8 and above you can use:
public void selectAnOption(String option) {
List<WebElement> choice = driver.findElements(By.xpath("your_xpath"));
choice
.stream()
.filter(e->e.getText().equals(option))
.findFirst().get().click();
}
Fixed it.. for anyone with a similar issue, I believe it lies in the fact that when this html code was developed, there were excess spaces (used for design purposes or fitting elements during development..maybe?) so I used .contains instead of .equals. duh!!
public void selectAnOption(String option) {
List<WebElement> choice = driver.findElements(By.xpath("//div[#class='atcui-scrollPanel-wrapper']/ul/li"));
for(WebElement e : choice){
System.out.println(e.getText());
if(e.getText().contains(option)){
e.click();
break;
}
}
}

Selenium Webdriver to wait until the asyn postback update panel request is completed

I have a table with clickable column header. On first click, the column should get sorted in ascending order and on second click column should get sorted in descending order.
The sorting has been implemented using async postback update-panel (I am not sure how it is done, it is an aspx page).
I would like to automate the sorting functionality using Selenium Webdriver. How can I implement the WAIT condition for the page where page doesn't get reloaded but only the table contents are reloaded.
waitForElementPresent wouldn't work, as no new element is displayed or hid on clicking the header.
PS: Java implentation required.
I have added a sample program that is related to a jquery table. Below is the flow of execution of the code:
First, it will navigate to the site.
Since I am taking the second column "Position" into consideration, it will retrieve first text under the column.
Then, click on the column header "Position" for sorting in ascending
Wait, 10 seconds(max), till the first text changes.
Print the result accordingly.
Again, click on the column header "Position" for sorting in descending
Wait, 10 seconds(max), till the first text changes.
Print the result accordingly.
public class TestSortTable{
static WebDriver driver;
public static void main(String[] args){
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.datatables.net/examples/basic_init/table_sorting.html");
//For Ascending in column "Position"
String result = clickAndWaitForChangeText(By.xpath("//table[#id='example']//th[2]"), By.xpath("//table[#id='example']//tr[1]/td[2]"), "Ascending");
if(result.contains("Fail"))
System.err.println(result);
else
System.out.println(result);
//For Descending in column "Position"
result = clickAndWaitForChangeText(By.xpath("//table[#id='example']//th[2]"), By.xpath("//table[#id='example']//tr[1]/td[2]"),"Descending");
if(result.contains("Fail"))
System.err.println(result);
else
System.out.println(result);
driver.close();//closing browser instance
}
//For clicking on header and waiting till the first text in the column changes
public static String clickAndWaitForChangeText(By Header_locator, By first_text_locator, String sortorder){
try{
String FirstText = driver.findElement(first_text_locator).getText();
System.out.println("Clicking on the header for sorting in: "+sortorder); //sortorder -> String representing sort order Ascending/Descending
driver.findElement(Header_locator).click();//Click for ascending/Descending
//Below code will wait till the First Text changes for ascending/descending
boolean b = new WebDriverWait(driver,10).until(ExpectedConditions.invisibilityOfElementWithText(first_text_locator, FirstText));
if(b==true){
return "Pass: Waiting Ends. Text has changed from '"+FirstText+"' to '"+driver.findElement(first_text_locator).getText()+"'";
}
else{
return "Fail: Waiting Ends. Text hasn't changed from '"+FirstText+"'.";
}
}catch(Throwable e){
return "Fail: Error while clicking and waiting for the text to change: "+e.getMessage();
}
}
}
NOTE:- You can use the method clickAndWaitForChangeText accordingly in your code for the relevant result(s).
You should wait until the JQuery.active retuns 0. Mine is written in c#. Addition to this, we can also wait for a specific element that you know will satisfy your wait criteria. You can use fluentWait or write your custom wait to wait until the element exists.
public void WaitForAjax()
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
wait.Until(d => (bool)(d as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0"));
}
EDIT: Java version
public void waitForAjaxLoad(WebDriver driver) throws InterruptedException{
JavascriptExecutor executor = (JavascriptExecutor)driver;
if((Boolean) executor.executeScript("return window.jQuery != undefined")){
while(!(Boolean) executor.executeScript("return jQuery.active == 0")){
Thread.sleep(1000);
}
}
return;
}
Directly taken from here

Categories