Selenium Java测试用例无法正常工作

时间:2014-01-08 08:44:58

标签: unit-testing testing selenium selenium-webdriver

我从FireFox创建了一个测试用例并执行了。它工作正常,

我将其导出为Java测试用例。我在Eclipse中运行,但它不起作用。我收到这样的错误:

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Incidents"}

我的测试用例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://myapphost:8080/" />
<title>IncidentsListTest22</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">IncidentsListTest22</td></tr>
</thead><tbody>
<tr>
    <td>open</td>
    <td>/myapp/login/auth?usrMsg=SE</td>
    <td></td>
</tr>
<tr>
    <td>type</td>
    <td>id=username</td>
    <td>username</td>
</tr>
<tr>
    <td>type</td>
    <td>id=password</td>
    <td>password</td>
</tr>
<tr>
    <td>clickAndWait</td>
    <td>id=loginForm_submit</td>
    <td></td>
</tr>
<tr>
    <td>clickAndWait</td>
    <td>link=Incidents</td>
    <td></td>
</tr>

</tbody></table>
</body>
</html>

MY POM

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>myapp-selenium-test</groupId>
    <artifactId>myapp-selenium-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <description>Tests the UI for rmsportal.</description>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.39.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>2.39.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>2.39.0</version>
        </dependency>
    </dependencies>
</project>

我的测试用例是:

@Test 
public void testIncidentsList() throws Exception { driver.get(baseUrl + "myapp/login/auth?usrMsg=SE"); driver.findElement(By.id("username")).clear();
  driver.findElement(By.id("username")).sendKeys("username");
  driver.findElement(By.id("password")).clear();
  driver.findElement(By.id("password")).sendKeys("password");
  driver.findElement(By.id("loginForm_submit")).click();
  driver.findElement(By.linkText("Incidents")).click(); 
}

HTML页面

<a class="dc-mega" href="#"> 
  Network Details 
  <span class="dc-mega-icon"></span> 
</a> 
<div class="sub-container non-mega" style="left: 138px; top: 31px; z-index: 1000;"> 
<ul class="sub" style="display: none;"> 
<li class="">
  <a href="/myapp/incident/list">Incidents</a> 
</li>

2 个答案:

答案 0 :(得分:1)

<a href="/myapp/incident/list">Incidents</a>ul标记的子项,对于webdriver似乎是不可见的(<ul class="sub" style="display: none;">)。你必须先让它可见......

答案 1 :(得分:0)

在找到元素时,Selenium在我的经历中非常不稳定。我建议尝试XLT,它有一个非常好的Firefox插件,并导出你可以运行的JUnit测试用例来测试你的页面。