How can i use driver.findelement for All Steps



  • I need to call google in a test case and with google open enter the information in the fields.

    Take a look at my code:

    import cucumber.api.PendingException;
    import cucumber.api.java.es.Dado;
    import cucumber.api.java.it.Quando;
    import cucumber.api.java.pt.Então;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import com.github.javafaker.Faker;
    import org.openqa.selenium.WebElement;
    

    public class Criar_Conta_Gmail {

    @Dado("^que eu esteja na pagina principal do gmail\\.$")
    public void que_eu_esteja_na_pagina_principal_do_gmail() throws Throwable {
        System.setProperty("webdriver.chrome.driver","C:\\Browsers\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.gmail.com");
        throw new PendingException();
    
      }
    
    @Quando("^criar uma nova conta com telefone invalido\\.$")
    public void criar_uma_nova_conta_com_telefone_invalido() throws Throwable {
    
           driver.findElement(By.xpath("//span[contains(text(),'Criar conta')]")).click();
           Thread.sleep(2000);
           driver.findElement(By.xpath("//div[contains(text(),'Para mim')]")).click();
           WebElement nomeusuario = driver.findElement(By.id("firstName"));
           Faker faker = new Faker();
           String Nome = faker.name().firstName();
           Thread.sleep(2000);
           nomeusuario.sendKeys(Nome);
          // driver.findElement(By.id("firstName")).sendKeys("Silvio Tadeu");       
    
        throw new PendingException();
    }
    
    @Então("^o sistema não permite a finalização do cadastro\\.$")
    public void o_sistema_não_permite_a_finalização_do_cadastro() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }
    

    enter image description here }



  • Creating a chrome driver instance in each step will create a new chrome session, so in step one if you have reached page to create a new user, in step 2 you will again just open a new browser.

    so open chrome driver in before all hook

    @Before
    public void doSomethingBefore() {
         WebDriver driver = new ChromeDriver();   
    
    }
    

    add remove it from individual steps

    https://cucumber.io/docs/cucumber/api/#hooks

    import cucumber.api.PendingException;
    import cucumber.api.java.es.Dado;
    import cucumber.api.java.it.Quando;
    import cucumber.api.java.pt.Então;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import com.github.javafaker.Faker;
    import org.openqa.selenium.WebElement;
    
    public class Criar_Conta_Gmail {
    
    WebDriver driver;
    @Before
    public void doSomethingBefore() {
         driver = new ChromeDriver();          
    }
    
    
    @Dado("^que eu esteja na pagina principal do gmail\\.$")
    public void que_eu_esteja_na_pagina_principal_do_gmail() throws Throwable {
        System.setProperty("webdriver.chrome.driver","C:\\Browsers\\chromedriver.exe");
        //WebDriver driver = new ChromeDriver();
        driver.get("https://www.gmail.com");
        driver.findElement(By.xpath("//span[contains(text(),'Criar conta')]")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//div[contains(text(),'Para mim')]")).click();
        throw new PendingException();
    
      }
    
    @Quando("^criar uma nova conta com telefone invalido\\.$")
    public void criar_uma_nova_conta_com_telefone_invalido() throws Throwable {
          // WebDriver driver = new ChromeDriver();
           WebElement nomeusuario = driver.findElement(By.id("firstName"));
           Faker faker = new Faker();
           String Nome = faker.name().firstName();
           Thread.sleep(2000);
           nomeusuario.sendKeys(Nome);
          // driver.findElement(By.id("firstName")).sendKeys("Silvio Tadeu");       
    
        throw new PendingException();
    }
    
    @Então("^o sistema não permite a finalização do cadastro\\.$")
    public void o_sistema_não_permite_a_finalização_do_cadastro() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }
    
    }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2