-2

I have a test case in which I have more than 30 steps, in all the steps I make the application to wait for some time (60 seconds) to load all the elements in the page, because of that it took more time for execution.

Here I need you guys to help in fine tuning.

Vijaya s
  • 31
  • 5
  • So what is the problem? If you wait 60 seconds each time of course it needs more execution time. – Camo Nov 04 '15 at 10:30
  • There is 2 ways to using wait in selenium, which one you use and how? – Leon Barkan Nov 04 '15 at 11:41
  • You shouldn't need to wait after each step and it definitely shouldn't be 60s *every* time. I have no idea how we are supposed to help you if you haven't shown us any code at all. – JeffC Nov 04 '15 at 20:51
  • In each step we are calling implicit wait function to wait for page load. As an example we have function name called, refereimplicitwait(), in each step we are calling the refereimplictiwait () funtion. – Vijaya s Nov 05 '15 at 06:39

1 Answers1

1

There are multiple ways of achieving waits with Selenium.

Thread.Sleep

Thread.Sleep is a static wait and it is not a good way to use in scripts as it is sleep without condition.

Thread.Sleep(2000); // this will wait for 2000 milliseconds

Explicit Waits

An explicit wait waits for a certain condition to occur before proceeding. For example it can be used when we want to check the visibility of an element before firing actions in to it.

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement DynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("DynamicElement")));

Implicit Wait

Implicit wait is used in cases where the WebDriver cannot locate an object immediately because of its unavailability. The WebDriver will wait for a specified implicit wait time and it will not try to find the element again during the specified time period.

Once the specified time limit is crossed, the webDriver will try to search the element once again for one last time. Upon success, it proceeds with the execution; upon failure, it throws exception.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://google.com");
WebElement DynamicElement = driver.findElement(By.id("DynamicElement"));

Fluent Wait

A FluentWait instance defines the maximum amount of time to wait for a condition to take place, as well as the frequency with which to check the existence of the object condition.

Wait wait = new FluentWait(driver).withTimeout(60, SECONDS).pollingEvery(10, SECONDS) .ignoring(NoSuchElementException.class);
WebElement dynamicelement = wait.until(new Function<webdriver,webElement>(){

public WebElement apply(WebDriver driver){
  return driver.findElement(By.id("dynamicelement"));
  }

});

You can use whatever method suites your situation.

debugger89
  • 2,108
  • 2
  • 14
  • 16