-1

I am a new bee for the automation and I have used the JUnit to develop the automation test script on using selenium webdriver on here my requirements

I have developed the automation test script. For some actions on my code, I want some time to execute the next command like find the element and load the pages so I use the Thread.sleep(). So now I found the info about using the Thread.sleep is a not good for the making test script

So my question is Can anyone say then how I give the dynamic wait time to my code My code is as below,

System.setProperty("webdriver.chrome.driver", "//path of the chrome driver");
driver = new ChromeDriver();

driver.get("https://url for my org");
Thread.sleep(5000); //to load the login page of my Org
driver.findElement(By.xpath("my login xpath")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("my login xpath")).sendKeys("username");
Thread.sleep(2000);
driver.findElement(By.xpath("my password xpath")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("my password xpath")).sendKeys("my password");
driver.findElement(By.xpath("login button")).click();
Thread.sleep(6000); //for giving the time to login Org

I have used the different type of the wait time on my code as per my requirement. If I go to the wait Class Can I able to achieve the same Wait time on there?

My question is different for the getting the explicit wait is provide the constant wait time to the different places like if we define the followingly,

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element_xpath")));
element.click();
//for another wait 
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element_xpath")));
element.sendKeys("something"); //Here it's wait time(10 seconds) is same to the after the click action wait time. 

So Why I am waiting the same time for all my wait section without need? if having another way please let me know?

If I am wrong please correct me.

Thanks.

Mohanraj Sivalingam
  • 231
  • 1
  • 8
  • 21
  • you may take your driver.findElement's into a thread and you can make join to master thread – mBogaz Jan 09 '18 at 12:06
  • @mBogaz thanks for your reply. And I am a new bee of automation I don't get your point. How I make my driver.findElement is to be the master thread. :( – Mohanraj Sivalingam Jan 09 '18 at 12:09
  • @mBogaz seriously don't understand your answer :( I ask how to give the different wait time to my code.So can you please give some explanation to the your posted answer. – Mohanraj Sivalingam Jan 09 '18 at 12:13
  • edited my post. – mBogaz Jan 09 '18 at 12:17
  • 2
    Possible duplicate of [Replace implicit wait with explicit wait (selenium webdriver & java)](https://stackoverflow.com/questions/45712431/replace-implicit-wait-with-explicit-wait-selenium-webdriver-java/45715759#45715759) – undetected Selenium Jan 09 '18 at 12:21
  • @mBogaz thanks for the reply. On your post how I know this code wait how long to execute the next line of the test script. I hope you don't mention that and where I give the time to wait for the control. – Mohanraj Sivalingam Jan 09 '18 at 12:22
  • @MohanRaj You can give a timeout for join, like t.join(5000); Its mean t works at most 5 sec. But if you want to know how much time takes, you should measure. But its not hard. You can get currentMillis at the beginnig and finishing in the thread. – mBogaz Jan 09 '18 at 12:29
  • @DebanjanB I update my question if any wrong in there please correct me. – Mohanraj Sivalingam Jan 09 '18 at 12:50
  • @DebanjanB thanks for your reply. but I have still one question @ M. Prokhorov to say the reason for why don't use thread.sleep but I struggle to get his point if you catch it can I know that for my future reference sir? – Mohanraj Sivalingam Jan 09 '18 at 13:26

3 Answers3

3

Firstly, I would very much like to advice you to use pageobjects or something like it. This is going to get you to maintenance-hell. Having said that, to solve your problem, you can use the implicit or explicit wait, whichever works best for you.

If you want to use the implicit wait, when initializing your driver, use:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

If you want to use explicit wait, use this example and make it your own:

WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement element = 
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));

element.click();

In both examples, the driver will wait for a max of 10 seconds. If you want to know the difference between the 2, check out the link and see what suits you best

http://toolsqa.com/selenium-webdriver/implicit-explicit-n-fluent-wait/

Anand
  • 1,899
  • 1
  • 13
  • 23
-1

You should add implicit wait in your program as shown below

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://url for my org");

Here Implicit waits are basically your way of telling WebDriver that it should wait 10 seconds(as mentioned in code) in cases if specified element not available on the UI (DOM), i.e. before throwing exception it will wait for 10 seconds.

Once you added implicit wait, remove all Thread.sleep(1000). It is not good practice to write Thread.sleep(1000) after every statement.

Or you can use explicit wait like

// Create object of WebDriverWait class
WebDriverWait wait=new WebDriverWait(driver,20);    
// Wait till the element is not visible    
WebElement element=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("ur xpath here")));
boolean status = element.isDisplayed();
//Then perform other action on element

This waits up to 20 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 20 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.

Shoaib Akhtar
  • 1,393
  • 5
  • 17
  • 46
  • thanks for clear reply to understand the answer correctly :) – Mohanraj Sivalingam Jan 09 '18 at 13:07
  • 1
    This is not a good practice. The Selenium contributors have specifically stated to NOT use implicit waits and the Selenium docs specifically state not to mix implicit and explicit waits as you have suggested. You should instead use explicit waits as needed. – JeffC Jan 09 '18 at 15:55
  • @JeffC I have edited my reply to use either implicit or explicit wait. I had used both in my program & it is working well, but thanks for pointing out that this is not a good practice & may cause issue – Shoaib Akhtar Jan 10 '18 at 06:15
-1

You should avoid using implicit wait due to the advice of Selenium contributors. You should also avoid mixing implicit and explicit waits as stated in the Selenium docs:

WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times.

You should instead use explicit waits, e.g. WebDriverWait. You shouldn't need a wait between entering data into fields. My general approach is to wait for a specific element to be visible on the page that indicates that a page is finished loading. You will want to pick one of the last elements to load on the page in the case where you have a long loading/dynamic section. After that element is loaded, you are free to enter text, etc. on the page at will. The only time you should need to wait again on the same page is if you trigger some dynamic behavior such as clicking a link that refreshes part of the page, making a selection from a dropdown that triggers a reload of another dropdown, etc.

Your code should look more like

driver.get("https://url for my org");
driver.findElement(By.xpath("my login xpath")).sendKeys("username");
driver.findElement(By.xpath("my password xpath")).sendKeys("my password");
driver.findElement(By.xpath("login button")).click();
// wait for element on newly loaded page and continue
  1. You don't need a wait after navigating to a URL. Selenium will wait for the browser readyState to be complete.

  2. You don't need to click in an element before using .sendKeys().

  3. You don't (generally) need a wait between entering data into fields.

JeffC
  • 22,180
  • 5
  • 32
  • 55