Sun
Sun

Reputation: 3564

Selenium Java Test Case not working

I created one test case from FireFox and executed. It working fine,

I exported that as Java test case. I run in Eclipse, but it not working. I am getting error like this:

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

My test case:

<?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 is

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

My test case is:

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

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

Upvotes: 0

Views: 458

Answers (2)

Grooveek
Grooveek

Reputation: 10094

<a href="/myapp/incident/list">Incidents</a> is a child of a ul tag that seems to be invisible (<ul class="sub" style="display: none;">) for webdriver. You'll have to make it visible first...

Upvotes: 1

RRZ Europe
RRZ Europe

Reputation: 954

Selenium is in my experience very flaky when finding elements. I recommend trying XLT which has a very good Firefox plugin and exports JUnit testcases that you can run to test your page.

Upvotes: 0

Related Questions