测试步骤
public class MilacronSmoke {
WebDriver driver;
@Given("^open firefox and start application$")
public void open_firefox_and_start_application() throws Throwable {
driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.get("https://example.com");
}
**Scenario 1**
@When("^I click on Login$")
public void I_click_on_Login() throws Throwable {
driver.findElement(By.xpath("//a[contains(.,'Login')]")).click();
}
@When("^enter valid \"([^\"]*)\" and valid \"([^\"]*)\"$")
public void enter_valid_and_valid(String uname, String pass) throws Throwable {
driver.findElement(By.id("Username")).sendKeys(uname);
driver.findElement(By.id("Password")).sendKeys(pass);
}
@Then("^Click on login and User should be able to login successfully$")
public void Click_on_login_and_User_should_be_able_to_login_successfully() throws Throwable {
driver.findElement(By.id("loginUser")).click();
}
**Scenario 2:**
@Given("^Click on shop for carts$")
public void Click_on_shop_for_carts() throws Throwable {
driver.findElement(By.xpath("//span[text()='Shop for Parts']")).click();
}
@Given("^select plates$")
public void select_plates() throws Throwable {
driver.findElement(By.xpath("//a[contains(.,'Plates ')]")).click();
}
@When("^I click on drsired product$")
public void I_click_on_drsired_product() throws Throwable {
driver.findElement(By.xpath("//a[@data-itemnumber='PLT01096096046']")).click();
}
@When("^click on item increment$")
public void click_on_item_increment() throws Throwable {
WebElement ele=driver.findElement(By.xpath("//i[contains(@class,'fa fa-caret-up')]"));
for(int i=0;i<=3;i++)
{
ele.click();
}
}
@When("^Click on buy now$")
public void Click_on_buy_now() throws Throwable {
driver.findElement(By.xpath("//button[contains(.,'Buy Now')]")).click();
}
@Then("^Product should be added to the cart successfully$")
public void Product_should_be_added_to_the_cart_successfully() throws Throwable {
}
功能文件 特征:测试米拉克朗烟雾情景
Scenario: Test login with valid credentials
Given open firefox and start application
When I click on Login
And enter valid "sshankar@genalpha.com" and valid "passw0rd"
Then Click on login and User should be able to login successfully
Scenario: Test shop for cart
Given Click on shop for carts
And select plates
When I click on drsired product
And click on item increment
And Click on buy now
Then Product should be added to the cart successfully
测试跑步者
@RunWith(Cucumber.class)
@Cucumber.Options(features="features",glue={"steps"})
public class TestRunnr {
当我运行这个黄瓜脚本时,它抛出一个NullPointer异常:
java.lang.NullPointerException
at steps.MilacronSmoke.Click_on_shop_for_carts(MilacronSmoke.java:47)
at ?.Given Click on shop for carts(MyApplication.feature:11)
第一种情况是成功执行,但第二种情况没有执行。我登录电子商务网站,尝试点击购买零件。
答案 0 :(得分:3)
每个方案都会创建所有步骤定义的新实例。
在您的情况下,您在Given step public void open_firefox_and_start_application()
中实例化驱动程序,以便第一个方案成功。
现在对于第二个场景,你的类的一个新实例有一个为null的webdriver,你没有调用前一步来实例化它。
您可以使用静态Web驱动程序,但是您将遇到并行测试的问题。如果您计划并行测试,请查找ThreadLocal
以确保您的webdriver已附加到特定线程。
另一种方法是将登录测试移动到单独的文件中。对于其他方案,将登录步骤移至Background
黄瓜标记。这样,webdriver将针对每个场景进行实例化。但是您需要决定是否要在各个方案中保持登录状态,删除cookie或为每个方案删除新浏览器。