Wait time to execute new action [Selenium] [C#]

Asked

Viewed 3,781 times

1

I’m starting with test automation with Selenium Webdriver in the language C#. I have a question, I have to do the test wait for the page to load to perform a new action. How do I do this? I believe that the easiest thing is to do for time. Someone can help me?

2 answers

2

There are 4 ways to wait.

Implicitly, it is applicable to all page elements.

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

Explicitly applied for a particular element.

WebDriverWait.until(CONDICAO_QUE_BATE_COM_O_ELEMENTO);

And for more specific cases.

WebDriverWait espera = new WebDriverWait(driver, 40);
WebElement element = espera.until(ExpectedConditions.elementToBeClickable(By.id("algumid")));

Using thread.

Thread.sleep(NUMBER_OF_MILLIS);

@source

1

Concrete use case, waiting execution time to perform new action:

The base form I wrote the test required providing name and email to submit. The test checks whether the name was provided after Submit by comparing the required field message not filled "Required Name Field" using the.getPageSource() driver. contains("Field name required")

I was hoping to confirm this display of this required field message on the screen after a Webelement.Ubmit() submission; and Test was failing, because assertTrue must have been running before the Pagesource message was read. So when using Thread.Sleep assert got through, see how the code turned out: ...

System.setProperty("webdriver.gecko.driver", "your path\\geckodriver.exe"); //Eu tive problema com o Firefox, só depois que baixei o geckdriver e coloquei essa linha que consegui rodar o teste no Firefox

WebDriver driver = new FirefoxDriver();

driver.get("path of your webapp");

WebElement email = driver.findElement(By.name("nome do campo email por exemplo"));

email.sendKeys("[email protected]");
email.submit(); \\sem fornecer o nome que era obrigatório

// Thread.sleep(NUMBER_OF_MILLIS);
Thread.sleep(3000); //Sem essa linha o teste não funcionava
assertTrue(driver.getPageSource().contains("Campo nome de preenchimento obrigatório"));

driver.close();

...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.