0

I am using the java selenium webdriver to create some web tests but the pages I must test are for the most part dynamically loaded. Luckily, events are triggered when certain content is loaded on the webpage.

I would like to detect these events using selenium webdriver but I do not think it is natively possible. I have checked out the doc for the EventFiringWebDriver but this looks like it will only work on selenium created events.

Should I make use of implicit waits instead?

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Guy
  • 46,488
  • 10
  • 44
  • 88
nickygs
  • 47
  • 8

1 Answers1

2

implicitlyWait means the driver will try to locate the element up to the specified amount of time, 10 seconds in your example. It is always good idea to add it and you only need to do it once for each WebDriver instance.

WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Note that implicitlyWait will make sure the element exists in the DOM. If you need to interact with the element it might not be good enough, you need the element to be visible. In those cases you can use explicit wait with Expected Conditions

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("id")));
// do something with the element

This will wait up to the specified amount of time to the element to be visible and will return the element just like driver.findElement. The wait object can be set one time, but the second line needs to be called each time you want to wait for a condition.

Guy
  • 46,488
  • 10
  • 44
  • 88
  • I read on the official document that implicit and explicit waiting cannot be used in conjunction as they give unexpected behavior. – nickygs Mar 28 '17 at 07:10
  • @nickygs Can you please share the link? because I never saw this anywhere and I never encountered any problem with using them both. – Guy Mar 28 '17 at 07:14
  • @nickygs Interesting, didn't see it before. However, I never encountered any problem. – Guy Mar 28 '17 at 10:16
  • maybe it's a leftover on their end. Back to the original topic, so events cannot be detected by the client? – nickygs Mar 29 '17 at 14:13
  • @nickygs not exactly a leftover, you can see good explanation why this is happaning in http://stackoverflow.com/questions/29474296/clarification-on-the-cause-of-mixing-implicit-and-explicit-waits-of-selenium-doc – Guy Mar 29 '17 at 15:14
  • @nickygs As for the question, Selenium can detect some events, but not those you are talking about. You have the waits for this. – Guy Mar 29 '17 at 15:15
  • from what I've seen you can create your own event listeners for logging but that's about it – nickygs Mar 30 '17 at 14:26