Selenium无法检测Checkout按钮并在购物车中添加产品

时间:2016-12-05 20:33:39

标签: selenium selenium-webdriver automation

问题是,Selenium无法检测Checkout按钮并在购物车中添加产品。

    package automationFramework;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Checkout {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before

    public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://sng.bestpricewebsitedesign.com/";
    driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS); 

  }

  @Test
  public void testNewCheckout() throws Exception {
    driver.get(baseUrl + "/index.php?route=common/home");
    driver.findElement(By.linkText("Home")).click();
    driver.findElement(By.linkText("Login")).click();
    driver.findElement(By.id("input-email")).clear();
    driver.findElement(By.id("input-email")).sendKeys("leo@abc.com");
    driver.findElement(By.id("input-password")).clear();
    driver.findElement(By.id("input-password")).sendKeys("asdfgh");
    driver.findElement(By.cssSelector("input.btn.btn-primary")).click();
    driver.findElement(By.linkText("Store")).click();
    driver.findElement(By.xpath("(//button[@type='button'])[14]")).click();
    driver.findElement(By.cssSelector("#cart > button.dropdown-toggle")).click();

    driver.findElement(By.id("button-payment-address")).click();
    driver.findElement(By.id("button-shipping-address")).click();
    driver.findElement(By.id("button-shipping-method")).click();
    driver.findElement(By.name("agree")).click();
    driver.findElement(By.id("button-payment-method")).click();
    driver.findElement(By.id("button-confirm")).click();
    driver.findElement(By.linkText("Continue")).click();
    driver.findElement(By.linkText("Logout")).click();
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}

1 个答案:

答案 0 :(得分:0)

以下是在购物车中添加'Blue Tshirt'并执行结帐的代码。

data : JSON.stringify(studentAnswers)

我已经测试了上面的代码,它对我来说很好。请用上面的测试方法替换现有的测试方法,如果适用,请告诉我。