【问题标题】:Problems with some Automation Testing using Before / After / Test使用之前/之后/测试的一些自动化测试的问题
【发布时间】:2017-08-01 11:28:00
【问题描述】:

使用 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 中编辑了这些东西,但它仍然无法正常工作......

【问题讨论】:

  • 我很困惑是错字还是别的什么:if (second >= 60) fail("timeout");?

标签: selenium selenium-webdriver


【解决方案1】:

亲爱的迈克尔·辛尼辛,

在我看来你有几个问题:

  1. 您没有向 Web 驱动程序提供有效的 URL

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

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

【讨论】:

    【解决方案2】:

    迈克尔·辛尼辛,

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

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多