使用Before / After / Test进行一些自动化测试的问题

时间:2014-09-29 11:08:36

标签: selenium selenium-webdriver

使用Before / After / Test进行一些自动化测试的问题     我陷入了浏览器窗口,没有执行任何测试...... 不知道为什么这不起作用。我从Selenium IDE导出了一些测试,导出为Java,在Eclipse中编辑了这些东西,但它仍然不起作用......

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 Checkimage {
  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://";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testCheckimage() throws Exception {
    driver.get("http://");
    for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if (isElementPresent(By.linkText("sign in / up"))) break; } catch (Exception e) {}
        Thread.sleep(1000);
    }

    driver.findElement(By.id("login-email")).clear();
    driver.findElement(By.id("login-email")).sendKeys("michael.sinitsin@avid.com");
    driver.findElement(By.id("login-password")).clear();
    driver.findElement(By.id("login-password")).sendKeys("mike1780");
    for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if ("sign in".equals(driver.findElement(By.id("login-buttons-password")).getText())) break; } catch (Exception e) {}
        Thread.sleep(1000);
    }

    driver.findElement(By.id("login-buttons-password")).click();
    for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if ("Tester".equals(driver.findElement(By.linkText("Tester")).getText())) break; } catch (Exception e) {}
        Thread.sleep(1000);
    }

    driver.findElement(By.linkText("Tester")).click();
    for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if (isElementPresent(By.id("inputScoreUrl"))) break; } catch (Exception e) {}
        Thread.sleep(1000);
    }

    driver.findElement(By.id("inputScoreUrl")).sendKeys("http://www.sheetmusicdirect.com/scorches/smd_000001.sco");
    for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if ("Enter a URL to a publicly accessible SCO file. No need to URL encode it, just copy and paste it in.".equals(driver.findElement(By.cssSelector("p.help-block")).getText())) break; } catch (Exception e) {}
        Thread.sleep(1000);
    }

    driver.findElement(By.id("submitScoreView")).click();
    for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if (isElementPresent(By.cssSelector("img"))) break; } catch (Exception e) {}
        Thread.sleep(1000);
    }

    try {
      assertTrue(isElementPresent(By.cssSelector("img")));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
  }

  @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;

不确定为什么这不起作用。我从Selenium IDE导出了一些测试,导出为Java,在Eclipse中编辑了这些东西,但它仍然不起作用......

2 个答案:

答案 0 :(得分:0)

亲爱的Michael Sinitsin,

我认为你有几个问题:

  1. 您没有为网络驱动程序提供有效的网址
  2.   

    替换driver.get(" http://");使用有效的网址,例如driver.get(" http://www.google.com");

    1. 您在公共网站上公开您的用户/密码

答案 1 :(得分:0)

迈克尔辛尼辛,

你首先要提到正确的URl driver.get(" http://");而且你的脚本太复杂了,你可以用最简单的方式编写它。