简体   繁体   中英

Wait with cucumber and not close browser

I'm starting at cucumber, I did an Elias Nogueira exercise and wanted to improve the result.

The URL exercise: http://eliasnogueira.com/arquivos_blog/selenium/desafio/2desafio/

What I want do improve:

1) I didn´t know a way to declare "WebDriverWait wait = new WebDriverWait(driver, 10);" just once. First I tried putting before setUp method, but I had problem "Failed to instantiate class" , so I decided to put within the methods that use it, but I know it wasn´t good in that way.

2) I wanted a way for the browser not close and reopen every time the test runs.

Here are my codes:

RunTests.java

package tests;
import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "funcionalidades", //pasta..
        glue = "tests", //pacote..
        plugin = {
                //gerar um relatorio do teste..
                "html:target/cucumber-html-report",
                "json:target/cucumber.json", "pretty:target/cucumber-pretty.txt",
            "usage:target/cucumber-usage.json", "junit:target/cucumber-results.xml"
    }
)
public class RunTests {

}

EdicaoInlineTestSteps.java

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

import static org.junit.Assert.*;


public class EdicaoInlineTestSteps {

    WebDriver driver;


    @Before
    public void setUp() throws Exception{
        System.setProperty("webdriver.chrome.driver", "C:\\caroltestes\\chromedriver.exe"); 
        driver = new ChromeDriver();



    }

    @After
    public void tearDown(){

        driver.quit();
    }



    @Given("^Acessar a página de edição inline do site do Elias Nogueira$")
    public void acessar_a_página_de_edição_inline_do_site_do_Elias_Nogueira() throws Throwable {
        driver.get("http://eliasnogueira.com/arquivos_blog/selenium/desafio/2desafio/");

    }

    @Given("^Alterar o nome \"(.*?)\"$")
    public void alterar_o_nome(String nome) throws Throwable {

        driver.findElement(By.id("name_rg_display_section")).click();
        driver.findElement(By.id("nome_pessoa")).clear();
        driver.findElement(By.id("nome_pessoa")).sendKeys(nome);


    }

    @When("^Completar a ação através do Salvar nome$")
    public void completar_a_ação_através_do_Salvar_nome() throws Throwable {
         driver.findElement(By.cssSelector("#name_hv_editing_section > input[value='Salvar']")).click();

    }

    @Then("^Verificar que a troca do nome foi feita com sucesso$")
    public void verificar_que_a_troca_do_nome_foi_feita_com_sucesso() throws Throwable {

        WebElement camponome = driver.findElement(By.id("name_rg_display_section")); 
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOf(camponome));

        String resultado = driver.findElement(By.id("name_rg_display_section")).getText();
        assertEquals(resultado, "Carol Cruz");

    }

    @Given("^Alterar o email \"(.*?)\"$")
    public void alterar_o_email(String email) throws Throwable {

        driver.findElement(By.id("email_rg_display_section")).click();
        driver.findElement(By.id("email_value")).clear();
        driver.findElement(By.id("email_value")).sendKeys(email);


    }

    @When("^Completar a ação através do Salvar email$")
    public void completar_a_ação_através_do_Salvar_email() throws Throwable {
        driver.findElement(By.cssSelector("#email_hv_editing_section > input[type=\"button\"]:nth-child(4)")).click();

    }

    @Then("^Verificar que a troca do email foi feita com sucesso$")
    public void verificar_que_a_troca_do_email_foi_feita_com_sucesso() throws Throwable {

        WebElement campoemail = driver.findElement(By.id("email_rg_display_section"));
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOf(campoemail));

        String mensagem2 = driver.findElement(By.id("email_rg_display_section")).getText();
        assertEquals (mensagem2 , "Email: lolicruz@gmail.com");

    }

    @Given("^Alterar o telefone \"(.*?)\"$")
    public void alterar_o_telefone(String telefone) throws Throwable {

        driver.findElement(By.id("phone_rg_display_section")).click();
        driver.findElement(By.id("phone_value")).clear();
        driver.findElement(By.id("phone_value")).sendKeys(telefone);

    }

    @When("^Completar a ação através do Salvar telefone$")
    public void completar_a_ação_através_do_Salvar_telefone() throws Throwable {
        driver.findElement(By.cssSelector("#phone_hv_editing_section > input[type=\"button\"]:nth-child(4)")).click();


    }

    @Then("^Verificar que a troca do telefone foi feita com sucesso$")
    public void verificar_que_a_troca_do_telefone_foi_feita_com_sucesso() throws Throwable {
        WebElement campotelefone = driver.findElement(By.id("phone_rg_display_section"));
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOf(campotelefone));

        String mensagem3 = driver.findElement(By.id("phone_rg_display_section")).getText();
        assertEquals (mensagem3 , "Telefone: 21 21222121");

    }

}

EdicaoInline.feature

#encoding: iso-8859-1

Feature: Edição Inline
    Eu quero realizar a edição inline do site do Elias Nogueira


Scenario: Realizando a alteração do nome
Given Acessar a página de edição inline do site do Elias Nogueira
    And Alterar o nome "Carol Cruz"
When Completar a ação através do Salvar nome
Then Verificar que a troca do nome foi feita com sucesso


Scenario: Realizando a alteração do email
Given Acessar a página de edição inline do site do Elias Nogueira
    And Alterar o email "lolicruz@gmail.com"
When Completar a ação através do Salvar email
Then Verificar que a troca do email foi feita com sucesso



Scenario: Realizando a alteração do telefone
Given Acessar a página de edição inline do site do Elias Nogueira
    And Alterar o telefone "21 21222121"
When Completar a ação através do Salvar telefone
Then Verificar que a troca do telefone foi feita com sucesso
public class EdicaoInlineTestSteps {

    WebDriver driver;
    WebDriverWait wait;

    @Before
    public void setUp() throws Exception{
        System.setProperty("webdriver.chrome.driver", 
        "C:\\caroltestes\\chromedriver.exe"); 
        driver = new ChromeDriver();
        wait = new WebdriverWait(driver,30);

}

At the moment you are using @Before and @After , these run once for each test, which makes the browser open and close multiple times, using @BeforeClass and @AfterClass would fix this. More information on this can be seen here .

We are using qaf you should give it try. It provides a way to reuse existing driver session in best efficient way.

It manages driver initialization and tear down for sequential and parallel executions. So you don't need to worry when to teardown or create driver. You can set that behavior using property selenium.singletone to define driver instance scope. So you can configure outside the code whether to have new driver(browser) session for each test or use the same driver session if exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM