每次执行 Scenario Outlines 的值时,浏览器都会重新加载

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

当我提供了 3 次无效的 phone 时,每次调用浏览器时都会加载该值。我使用了背景,但我还没有做到。 谢谢

特征文件:

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.

代码:

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

        }

Scenario outline - 可用于运行多次使用不同值的相同场景,该场景对每一行执行一次,每一行都被视为一个场景

Background - 表示将 运行 用于每个场景的通用步骤

您可以做的是检查驱动程序是否已经创建并重新使用它,但请记住,这不是一个好的做法。

其他需要考虑的改进是使用 hooks 将驱动程序 start/close 移到 steps/scenario 之外,并且每一步都只做它所说的。