2016-08-23 98 views
1

我在本地主机中测试了我的maven测试。一天前一切都很好。如果包含单词,我会在主页上测试标题。如果包含相同的单词,则返回true,否则返回false,但每次出现故障即使我的标题良好。这很奇怪,因为我没有改变任何东西。Selenium得到标题

package Testselenium; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.Assert; 
import org.testng.annotations.AfterTest; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Test; 

public class NewTest {  

    private WebDriver driver; 

    @Test    
    public void testEasy() {      
     driver.get("127.0.0.1/Demo"); 
     String title = driver.getTitle(); 
     Assert.assertTrue(title.contains("TimDevops")); 
    } 

    @BeforeTest 
    public void beforeTest() { 
     driver = new FirefoxDriver(); 
    } 

    @AfterTest 
    public void afterTest() { 
     driver.close(); 
    } 
} 

然后我的文件开始进行测试:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="utf-8"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 
<meta name="description" content=""> 
<meta name="author" content=""> 
<link rel="icon" href="../../favicon.ico"> 

<title>Bienvenue sur TimDevOps</title> 

<!-- Bootstrap core CSS --> 
<link href="style.css" rel="stylesheet"> 

下面我的pom.xml

<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>Devops</groupId> 
<artifactId>Devops</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<build> 
<plugins> 
    <plugin> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <version>2.19.1</version> 
     <inherited>true</inherited> 
     <configuration> 
      <suiteXmlFiles> 
       <suiteXmlFile>testng.xml</suiteXmlFile> 
      </suiteXmlFiles> 
     </configuration> 
    </plugin> 
    </plugins> 
</build> 
<distributionManagement> 
    <!-- Publish snapshots here --> 
    <snapshotRepository> 
      <id>nexus</id> 
      <name>My snapshots</name> 
      <url>http://localhost:8081/repository/maven-public/</url> 
    </snapshotRepository> 
</distributionManagement>    
<dependencies> 
    <!-- http://mvnrepository.com/artifact/org.apache.maven.surefire/surefire-api --> 
    <dependency> 
     <groupId>org.apache.maven.surefire</groupId> 
     <artifactId>surefire-api</artifactId> 
     <version>2.19.1</version> 
    </dependency> 
    <!-- http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin --> 
    <dependency> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.5.1</version> 
    </dependency> 
    <!-- <dependency>    
     <groupId>junit</groupId>        
     <artifactId>junit</artifactId> 
     <version>3.8.1</version>        
     <scope>test</scope>         
    </dependency> -->    
    <dependency>    
     <groupId>org.seleniumhq.selenium</groupId>        
     <artifactId>selenium-java</artifactId>        
     <version>2.53.1</version>        
    </dependency>    
    <dependency>    
     <groupId>org.testng</groupId>        
     <artifactId>testng</artifactId>        
     <version>6.9.8</version>        
     <scope>test</scope>         
    </dependency> 
</dependencies> 
</project> 

这是我构建的结果:

[TestNG] Running: 
C:\Users\TIMSPIRIT\workspace\Devops\testng.xml 


=============================================== 
Suite 
Total tests run: 1, Failures: 1, Skips: 0 
=============================================== 

有人可以帮我吗?

+0

您的代码包含_Assert.assertTrue(title.contains(“TimDevops”)); _标题是_ Bienvenue sur TimDevOps _。请检查这是因为字符串应该是平等的。 – JDelorean

+0

另外,我知道这是一个本地网页,不过你应该在'driver.open()'之后实现某种'waitFor()'方法。否则在Selenium打开浏览器/加载页面之前会抛出异常。 – JDelorean

+0

@JDelorean感​​谢您的回答。我不知道我怎么能把方法waitFor()。 – Djoh

回答

0

您首先获取标题,然后在方法testEasy()中导航到您的网站。应该是相反的。

+0

谢谢你的回答。我更新我的文件并编辑帖子,但它仍然是相同的 – Djoh

+0

你能打印出变量标题的内容什么webdriver为你获得 – Grasshopper

+0

谢谢你的答案。我尝试返回(标题),但我不知道我必须把这个命令。你可以点亮我吗? – Djoh