Feature: Test Milacron Smoke scenario
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 Click on Add to cart
Then product should be added in the cart successfully
Runner.class
package runner;
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@Cucumber.Options(features="features",glue={"steps"}, format = {"pretty", "html:target/Destination"})
public class TestRunnr {
}
StepDefinition.class
public class MilacronSmoke {
static WebDriver driver;
@Given("^open firefox and start application$")
public void open_firefox_and_start_application() throws Throwable {
driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
driver.get("https://milacronqa.genalphacatalog.com");
}
@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();
}
@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("^Click on Add to cart$")
public void Click_on_Add_to_cart() throws Throwable {
driver.findElement(By.xpath(".//*[@id='product-catalog-hldr']/div[1]/div[4]/button[1]")).click();
}
@Then("^product should be added in the cart successfully$")
public void product_should_be_added_in_the_cart_successfully() throws Throwable {
WebElement element = driver.findElement(By.xpath("//a[contains(.,'Cart1')]"));
element.click();
WebElement element1=driver.findElement(By.xpath("//a[contains(.,'Item # 10010773')]"));
String str=element1.getText();
System.out.println(str);
Assert.assertEquals("Item # 10010773", str);
System.out.println("Test passed successfully");
}
}
当任何产品已经添加到购物车中时,它不会点击购物车并且抛出元素是可点击的例外,如果购物车为空,则此脚本工作正常如下:
org.openqa.selenium.WebDriverException:元素在点(1193,52)处不可点击。其他元素将收到点击: 命令持续时间或超时:76毫秒 构建信息:版本:'2.53.1',修订版:'a36b8b1',时间:'2016-06-30 17:32:46' 系统信息:主机:'WLL16GJ7',ip:'10 .1.18.240',os.name:'Windows 10',os.arch:'amd64',os.version:'10 .0',java.version:'1.8.0_121 “ 驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver Capabilities [{applicationCacheEnabled = true,rotate = false,handlesAlerts = true,databaseEnabled = true,version = 47.0.1,platform = WINDOWS,nativeEvents = false,acceptSslCerts = true,webStorageEnabled = true,locationContextEnabled = true,browserName = firefox,takesScreenshot = true,javascriptEnabled = true,cssSelectorsEnabled = true}] 会话ID:95fda63b-abc3-4758-825d-223e67522d86 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 在org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) 在org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) 在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678) 在org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327) 在org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:85) at steps.MilacronSmoke.product_should_be_added_in_the_cart_successfully(MilacronSmoke.java:75) 在?。然后,产品应成功添加到购物车中(MyApplication.feature:13)
答案 0 :(得分:0)
我不确定实际的Java代码,但是尝试引入等待几毫秒(或等待,如果你希望它在需要的时候继续完成),以确保带有xpath的元素://div[contains(@class, 'pageLoader')]
不再显示在页面上。
我感觉它点击了添加按钮并立即尝试点击购物车,但它不能,因为这个pageLoader元素已经挡住了。
修改强>
我做了一点搜索,找到了this,看起来就像我在谈论的那样。
特别是这些部分:
import org.openqa.selenium.support.ui.WebDriverWait;
并且
WebDriverWait wait = new WebDriverWait(webDriver, 1);
wait.until(ExpectedConditions.elementToBeClickable(By.id("celcius")));
当然,您的代码更像是这样:
@Then("^product should be added in the cart successfully$")
public void product_should_be_added_in_the_cart_successfully() throws Throwable {
// THIS IS THE NEW BIT
WebDriverWait wait = new WebDriverWait(webDriver, 1);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Cart1')]")));
// UNTIL HERE
WebElement element = driver.findElement(By.xpath("//a[contains(.,'Cart1')]"));
element.click();
WebElement element1=driver.findElement(By.xpath("//a[contains(.,'Item # 10010773')]"));
String str=element1.getText();
System.out.println(str);
Assert.assertEquals("Item # 10010773", str);
System.out.println("Test passed successfully");
}
请记住在步骤定义文件中包含include语句