2

我是一个新的测试人员,我有两个关于我的测试脚本的问题,我从 selenium ide 导出我的测试脚本(导出到 Junit4 webdriver),然后将它粘贴到我的 eclipse indigo 中。但是当我运行我的测试脚本时,它只会运行到
driver.findElement(By.name("j_idt61")).click();

在那行代码之后实际上又多了一行,即单击注销链接driver.findElement(By.linkText("Logout")).click();

但硒似乎找不到linkText“ Logout”..

这是我的代码

package admin ;

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 login {
  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 = " here I put my website URL";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testLogin() throws Exception {
    driver.get(baseUrl + "/admin/pages/login.xhtml");

    driver.findElement(By.id("j_username")).click();
    driver.findElement(By.id("j_username")).clear();
    driver.findElement(By.id("j_username")).sendKeys("sasa");
    driver.findElement(By.id("j_password")).click();
    driver.findElement(By.id("j_password")).clear();
    driver.findElement(By.id("j_password")).sendKeys("sasasamsudin");
    driver.findElement(By.name("j_idt61")).click();
    driver.findElement(By.linkText("Logout")).click();
  }

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

 public boolean isElementPresent(By id) {
    try {
      driver.findElements(By.id("J_idt16:J_idt30"));
      return true;
    } catch (org.openqa.selenium.NoSuchElementException e) {
      return false;
    }
  }

 public boolean isElementVisible(By id)
 {

     return driver.findElement(By.id("J_idt16:J_idt30")).isDisplayed();

 }

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

请任何人帮助我。我刚开始我的工作,真的需要这个自动化测试才能正常工作..谢谢:)

4

3 回答 3

3

尝试driver.findElement(By.partialLinkText("Logout"));改用,因为可能有您看不到的其他/空白字符。partialLinkText()仅检查链接是否包含您的文本,但linkText()会检查链接的整个文本是否等于您的文本。

此外,请确保包含文本“Logout”的元素是<a></a>- 因为linkText()and partialLinkText() 不会检查任何其他类型的元素。

编辑:

如果问题是元素存在但不可见,那么您需要等待它变得可见:

// find the element
WebElement anchor = driver.findElement(By.partialLinkText("Logout"));

// create a wait with 10-second timeout
WebDriverWait wait = new WebDriverWait(driver, 10);

// wait for the element to become visible
anchor = wait.until(ExpectedConditions.visibilityOf(anchor));

// proceed with test...

或者,如果元素最终不会自行变得可见,则可以强制它可见:

// find the element
WebElement anchor = driver.findElement(By.partialLinkText("Logout"));

// execute JavaScript to change the element's visibility
((JavascriptExecutor)driver).executeScript("arguments[0].style.visibility = 'visible'; arguments[0].style.display = 'block';", anchor);

// proceed with test...
于 2013-06-05T17:00:30.953 回答
0

你可能有这些问题之一:

1-linkText="logout" 的组件尚未启用,因此您应该等到组件启用

2-不鼓励使用 classname 或 linkedtext... 因为它可能出现在页面的其他位置,所以最好使用 By.ID 或 By.XPath

3-组件“LogOut”应与之前的组件在同一框架中

祝你好运 :-)

于 2014-06-23T11:30:15.487 回答
0

我自己也遇到了这个问题。我导出了一组在 Selenium IDE 中运行良好的测试,但导出时似乎不再运行。您必须原谅我的代码示例,因为我使用的是 Python 而不是 Java。所以就我而言,我有

driver.find_element_by_link_text("Logout").click()

我改成这个

driver.find_element_by_xpath("//a[contains(text(),'Logout')]").click()

每当您使用“By.LINK_TEXT”时,这同样适用,只需将其交换为“By.XPATH”,然后使用基于 xpath 的参数更新参数。

希望有帮助。

F先生

于 2014-06-23T11:09:32.000 回答