我需要点击退房页面上的“Countine”按钮地址。但是无法点击它

时间:2016-12-13 10:38:12

标签: java selenium selenium-webdriver webdriver automated-tests

我已尝试过所有内容,似乎没有任何问题可以解决问题。无法点击结帐页面上的地址继续按钮。

我写了下面的代码,但是webdriver无法点击“继续”按钮请帮忙。

package automationFramework;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.annotations.*;

import static org.testng.Assert.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

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

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

  @Test
  public void testNewCheckout() throws Exception {
    driver.get(baseUrl + "/index.php?route=common/home");
    driver.findElement(By.cssSelector("img")).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.get("http://sng.bestpricewebsitedesign.com/index.php?route=product/category&path=59");
    //driver.manage().window().maximize();
    //driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
    driver.findElement(By.xpath("//h4/a[text()='Yellow Tshirt']/following::span[text()='Add to Cart'][1]")).click();`    

driver.findElement(By.xpath("html/body/header/div[1]/div/div[2]/div[1]/ul/li[3]/a")).click();

我写了下面的代码,但是webdriver无法点击“继续”按钮请帮忙。(按钮付款地址无效)。

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

  @AfterClass(alwaysRun = true)
  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;
    }
  }
}

2 个答案:

答案 0 :(得分:0)

尝试以下方式捕获异常并打印它,异常将显示原因。否则你想知道原因。大多数时候理由是元素不存在。

public void testNewCheckout(){

  try {

    driver.findElement(By.id("button-payment-address")).click();
    driver.findElement(By.id("button-shipping-address")).click();


   } catch (Exception e) {

      e.printStackTrace();           

   }

}

答案 1 :(得分:0)

尝试使用xpath作为按钮。用以下内容替换现有的 testNewCheckout()方法:

@Test
public void testNewCheckout() throws Exception {
    driver.get(baseUrl + "/index.php?route=common/home");
    driver.findElement(By.cssSelector("img")).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.get("http://sng.bestpricewebsitedesign.com/index.php?route=product/category&path=59");
   //driver.manage().window().maximize();
   //driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
   driver.findElement(By.xpath("//h4/a[text()='Yellow Tshirt']/following::span[text()='Add to Cart'][1]")).click();`    

   driver.findElement(By.xpath("html/body/header/div[1]/div/div[2]/div[1]/ul/li[3]/a")).click();

   //driver.findElement(By.id("button-payment-address")).click();
   driver.findElement(By.xpath("//input[@value='Continue']")).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();
}

它对我有用。