-3

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 }

1
  • 1
    Isn't this question of the same nature as the one about making variables available to all the steps?
    – Alexey R.
    Feb 18 '20 at 17:46
1

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

}
10
  • Explaining better, I didn't get it right !. I want to call the browser in one step, and with the browser open I want to enter the field information from the other step. Feb 18 '20 at 16:12
  • @RebekaMontana see the edited answer
    – PDHide
    Feb 18 '20 at 16:12
  • before hook will run before the first step in a scenario. so you will creat a browser session and use it through out your scenario unlike restarting browser each time as you did
    – PDHide
    Feb 18 '20 at 16:14
  • Did not work!!.. I'll adjust my code and show what I need. Feb 18 '20 at 16:27
  • Done, now I explained better. Feb 18 '20 at 16:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.