Reputation: 13
I'm writing some test in Selenium and I've got one, annoying problem. In my code Thread.sleep repeats in every second line because I need a short sleep after every function in method. How to avoid it. I don't like my code. It looks very sloppily. I wanna change this repeating Thread.sleep for something more optimal. Here is my code:
@Test
public void shouldDownloadDriver() throws InterruptedException{
driver.get("https://www.selenium.dev/");
driver.manage().window().maximize();
Thread.sleep(1500);
driver.findElement(By.xpath("//*[@id=\"main_navbar\"]/ul/li[4]/a/span")).click();
Thread.sleep(1500);
driver.findElement(By.xpath("//*[@id=\"m-documentationgetting_started\"]/span")).click();
Thread.sleep(1500);
driver.findElement(By.xpath("//*[@id=\"m-documentationgetting_startedinstalling_browser_drivers\"]/span")).click();
Thread.sleep(1500);
((JavascriptExecutor) driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get("https://chromedriver.storage.googleapis.com/index.html");
Thread.sleep(1500);
driver.findElement(By.xpath("/html/body/table/tbody/tr[100]/td[2]/a")).click();
Thread.sleep(1500);
driver.findElement(By.xpath("/html/body/table/tbody/tr[4]/td[2]/a")).click();
Thread.sleep(2000);
driver.switchTo().window(tabs.get(0));
Thread.sleep(3000);
printSuccess();
}
Upvotes: 1
Views: 690
Reputation: 2922
visibilityOfElementLocated: Returns the WebElement once it is located and visible.
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"main_navbar\"]/ul/li[4]/a/span")));
driver.findElement(By.xpath("//*[@id=\"main_navbar\"]/ul/li[4]/a/span")).click();
presenceOfElementLocated: Returns the WebElement if element is present on DOM and not even visible.
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"main_navbar\"]/ul/li[4]/a/span")));
driver.findElement(By.xpath("//*[@id=\"main_navbar\"]/ul/li[4]/a/span")).click();
Upvotes: 1