繁体   English   中英

Selenium Java测试用例不起作用

[英]Selenium Java Test Case not working

我从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>

我的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>

<a href="/myapp/incident/list">Incidents</a>ul标记的子代,对于ul来说似乎是不可见的( <ul class="sub" style="display: none;"> ) 。 您必须先使其可见...

根据我的经验,硒在发现元素时非常易碎。 我建议您尝试使用XLT ,它具有一个非常好的Firefox插件,并且可以导出JUnit测试用例,您可以运行这些用例来测试您的页面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM