2016-08-23 32 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