等黄瓜,不要关闭浏览器

时间:2017-08-28 14:24:08

标签: java selenium cucumber

我从黄瓜开始,我做了一个 Elias Nogueira 练习,并希望改善结果。

网址练习: http://eliasnogueira.com/arquivos_blog/selenium/desafio/2desafio/

我想要做些改进:

1)我不知道如何宣布“WebDriverWait wait = new WebDriverWait(driver,10);”一次。 首先我尝试在setUp方法之前放置,但是我遇到了问题“无法实例化类”,所以我决定把它放在使用它的方法中,但我知道这样做并不好。

2)我想要一种不关闭浏览器的方法,并在每次测试运行时重新打开。

以下是我的代码:

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

3 个答案:

答案 0 :(得分:1)

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);

}

答案 1 :(得分:0)

当您使用@Before@After时,每次测试运行一次,这会使浏览器多次打开和关闭,使用@BeforeClass@AfterClass会解决这个问题。有关这方面的更多信息,请参见here

答案 2 :(得分:0)

我们正在使用qaf你应该尝试一下。它提供了一种以最有效的方式重用现有驱动程序会话的方法。

它管理驱动程序初始化和拆除顺序和并行执行。因此,您不必担心何时拆卸或创建驱动程序。您可以使用属性selenium.singletone设置该行为以定义驱动程序实例范围。因此,您可以在代码外部配置是否为每个测试设置新的驱动程序(浏览器)会话,或者如果存在则使用相同的驱动程序会话。

相关问题