1

I have created a script in Selenium IDE that goes to this page: https://www.lexology.com/instructcounsel/find and presses the 'Example' button and then asserts that results are displayed for the search. This script runs without a problem but when I export as Java to Selenium GRID then the assert fails.

The image taken by screenshot.java shows that none of the search results have loaded: enter image description here

I am expecting: enter image description here

This seems to be the case even if I add a wait for the element. I have tried also adding a js.executeScript but this hasn't helped. It looks like either the button is not being found and clicked or the asynchronous content is not loading.

Here is my exported script:

package com.lbr.testcases.lexology.experts.instruct_counsel;

import com.lbr.utils.Screenshot;
import java.util.*;
import java.net.MalformedURLException;
import java.net.URL;
import static org.hamcrest.CoreMatchers.is;
import org.hamcrest.MatcherAssert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.WebDriver;

public class instruct_counsel_example_anon {
  private WebDriver driver;
  JavascriptExecutor js;

  @Before
  public void setUp() throws MalformedURLException {
    driver = new RemoteWebDriver(new URL("http://host.docker.internal:4444/wd/hub"), new ChromeOptions());
    js = (JavascriptExecutor) driver;
    new HashMap<String, Object>();
  }
  @After
  public void tearDown() {
    Screenshot.TakeScreenshot(driver);
    driver.quit();
  }
  @Test
  public void searchinstructcounselexampleanon() {
    // GIVEN that I am an anonymous user on the Instruct Counsel page
    driver.get("https://www.lexology.com/instructcounsel/find");
    // WHEN I press the Example button
    driver.findElement(By.xpath("//div[@id='do-example-search']/button")).click();
    js.executeScript("window.scrollTo(0,579)");
    // adding a wait for the content to be displayed
    WebDriverWait wait = new WebDriverWait (driver, 60);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[contains(.,\\\'3. Find your lawyer (746 matches found)\\\')]")));
    // THEN some example lawyers are displayed
    MatcherAssert.assertThat(driver.findElement(By.xpath("//h3[contains(.,\'David Zetoony\')]")).getText(), is("David Zetoony"));
    MatcherAssert.assertThat(driver.findElement(By.xpath("//h3[contains(.,\'3. Find your lawyer (746 matches found)\')]")).getText(), is("3. Find your lawyer (746 matches found)"));
  }
}

1 Answer 1

1
// GIVEN that I am an anonymous user on the Instruct Counsel page
driver.get("https://www.lexology.com/instructcounsel/find");
// WHEN I press the Example button
driver.findElement(By.xpath("//div[@id='do-example-search']/button")).click();

driver.get should wait for the page to load, but that doesn't mean the button is ready to be clicked (the page can 'load' just the JavaScript hasn't finished registering onClick events for example). Try adding a wait for elementToBeClickable. You can also try putting in thread.sleep to see if you're just going to fast. Just don't forget to replace them with waits conditions!

1
  • I have moved the elementToBeClickable wait: driver.get("https://test1.lexology.com/instructcounsel/find"); WebDriverWait wait = new WebDriverWait (driver, 30); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='do-example-search']/button"))); // WHEN I press the Example button driver.findElement(By.xpath("//div[@id='do-example-search']/button")).click(); I still get the same issue. Commented Apr 15, 2021 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.