Determine if a page has changed using Webdriver

Sometimes the visible content of web pages depends on the state of the input elements of the page (for example, the “Choose your university” input element may appear only after you select “Student” in the “Work” field) or by clicking on some page elements (like buttons of a drop-down menu ) I am trying to automate the process of determining such page element dependencies using Selenium Webdriver. The first idea is to enter some things into the text input fields, set checkboxes with a tick / uncheck a box, click some buttons / links, etc. And see if any elements appeared or disappeared on the page. Problems:

  • Is there an easy way to find out if something has appeared on the page? Well, I can make a map from the elements of the web page into the states {'visible', 'invisible'} after each change and find out if something has changed, but is there something built-in for this purpose?

  • Pressing some buttons may cause another page to load, and I want to stay on the page I'm testing, is there a way to determine if the click () method caused another page to load and prevent it?

+6
source share
3 answers

Divi's answer to question 2 is fine. For part 1, you can check if the item is displayed as you said:

webElement.isDisplayed(); 

Or you can compare html pages before and after the event.

 driver.getPageSource(); 

(Perhaps you can also check the URL if this changes after each event).

The only problem with getting the html source is that the event may have occurred after your last action and did not end, leaving the html the same. To get around this, you can check out some of the following conditions:

 (Boolean) ((JavascriptExecutor)driver).executeScript("return jQuery.ready"); ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete"); 

Or you can use another javascript framework that has a different function similar to jQuery.ready.

Finally, you may have a problem with triggering an event only after you change the active element, make a tab from the field, etc. So you can run something like:

 Actions actionObject = new Actions(driver); actionObject.sendKeys(Key.TAB).perform(); 

Or you can use javascript to install it (I did not check the code below, and I would install webElement in the html base element):

 ((JavascriptExecutor) driver).executeScript("arguments[0].focus();", webElement); 

I do not suggest using webdriver to click on the body element, as this may cancel your last click.

I include the Divi answer for Question 2 for completeness:

We can check if it is redirected to another page or not by receiving the URL of the page and comparing it with the expected URL

 WebDriver driver; driver.getCurrentUrl() // will returns the String value 

if your current url does not match your expected url, you go in two ways

1) You can open the expected URL again using the Open function in selenium (or) webdriver navigation mode

 driver.navigate().to("expected_url"); 

2) You can use reverse navigation

 driver.navigate().back(); 
+5
source

Based on James answer, and since our test site does not use jQuery, the following worked for me:

 public static class WebDriverExtensions { public static void WaitForPage(this IWebDriver driver) { WaitForPage(driver, 60, 200); } private static void WaitForPage(IWebDriver driver, int seconds, int waitFor) { while (!((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete")) { System.Threading.Thread.Sleep(50); } bool stillChanging = true; int iterations = 0; int maxIterations = (1000 * seconds) / waitFor; string previous = ""; string current = ""; while (stillChanging && iterations < maxIterations) { current = driver.PageSource; System.Threading.Thread.Sleep(waitFor); stillChanging = (!previous.Equals(current)); previous = current; } } public static void Click(this IWebElement el, IWebDriver driver) { el.Click(); driver.WaitForPage(); } } 

What I do in my code is calling el.Click (driver) instead of el.Click (). The method will return after the page stops changing. You can also call the public driver.WaitForPage () method before / after changes that might change something on your page. The hard-coded values ​​must be changed to ensure that the maximum latency and the period between validation changes work as expected. Make waitFor too small and you won't find any changes. Make it too big and you have to wait a long time after each click.

+3
source

Good for your first question to check visibility in Java

 Selenium selenium; selenium.isVisible("id=element_id"); // it will return boolean value whether the element is present or not 

If you want to use the webDriver tool, use

 RenderedWebElement webElement=findElement(By.id("element_id")); webElement.isDisplayed(); 

For your second question

We can check if it is redirected to another page or not by receiving the URL of the page and comparing it with the expected URL

 WebDriver driver; driver.getCurrentUrl() // will returns the String value 

if your current url does not match your expected url, you go in two ways

1) You can open the expected URL again using the Open function in selenium (or) webdriver navigation mode

 driver.navigate().to("expected_url"); 

2) You can use reverse navigation

 driver.navigate().back(); 
+1
source

Source: https://habr.com/ru/post/923774/


All Articles