Search code examples
javaselenium-webdriverbddcucumber-junit

Every time a value of Scenario Outlines is executed, the browser loads again


When I provide the invalid phone three times, each time the value is called the Browser is loaded. I used Background but I haven't been able to do it yet. Thank You

Feature file:

Functionality: Validate the creation of a new Gmail account.

Background:
  Given that I'm on the gmail main page.

Scenario Scheme: Register user with invalid phone
 When I create a new account with phone "" different from my country.
 Then I can see the message "" in an invalid format.

Examples:

| phone           | message |  
| +374 981929578  | This phone number format is not valid. Check the country and the number. |  
| +61 981929578   | This phone number format is not valid. Check the country and the number. |
| +36 981929578   | This phone number format is not valid. Check the country and the number.

Code:

import cucumber.api.java.pt.When;
import cucumber.api.java.pt.Then;
import cucumber.api.java.pt.When;
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.firefox.FirefoxDriver;

    public class newUser {

    WebDriver driver;

    @Given("^that I am on the main page of gmail\\.$")
    public void that_i_am_on_the_main__page_of_gmail() throws Throwable

      {
        System.setProperty("webdriver.chrome.driver","C:\\Browsers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.gmail.com");  
      }

    @When("^I create a new account with phone \ "([^ \"] *) \ "different from my Country \\.$")
    public void i_create_a_new_account_with_differente_phone_from_my_country(String phone) throws Throwable {

          driver.findElement(By.xpath("//span[contains(text(),'create account ')]")).click();
          Thread.sleep(2000);
          driver.findElement(By.xpath("//div[contains(text(),'to me')]")).click();
          Thread.sleep(2000);
          driver.findElement(By.id("firstName")).sendKeys("Jhon");
          driver.findElement(By.id("lastName")).sendKeys("Automation");
          driver.findElement(By.id("username")).sendKeys("jhonautomation2020@gmail.com");


          driver.findElement(By.name("Passwd")).sendKeys("automation2020");
          driver.findElement(By.name("ConfirmPasswd")).sendKeys("automation2020");


          driver.findElement(By.id("accountDetailsNext")).click();
          Thread.sleep(3000);


          driver.findElement(By.id("phoneNumberId")).sendKeys(phone);
          driver.findElement(By.id("gradsIdvPhoneNext")).click();

        }

Solution

  • Scenario outline - can be used to run the same scenario multiple times with different values, the scenario is executed one time for each line and each line is considered as a scenario

    Background - represents a common step that will run for each scenario

    What you can do is to check if the driver is already created and reuse it, but remember that this is not a good practice.

    Other things to consider as improvements are to move the driver start/close outside the steps/scenario using hooks and each step to do only what it says is doing.