[当我提供三遍无效电话时,每次调用该值时都会加载浏览器。我使用了Background,但还无法做到这一点。谢谢
功能文件:
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("[email protected]");
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
将驱动程序的启动/关闭移动到步骤/场景之外,并且每个步骤仅执行其正在执行的操作。