2016-08-23 2 views
1

У меня ошибка с моим тестом maven в localhost. Один день назад все было в порядке. Я тестирую заголовок на моей домашней странице, если в нем есть слово. Если содержит одно и то же слово, возвращайте true else 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

Кроме того, я понимаю, что это локальная веб-страница, но вы должны реализовать какой-то метод 'waitFor()' после 'driver.open()'. В противном случае будет исключено исключение, прежде чем Selenium сможет открыть браузер/загрузить страницу. – JDelorean

+0

@JDelorean благодарю вас за ответ. Я не знаю, как я могу поместить метод waitFor(). – Djoh

ответ

0

Вы сначала получаете заголовок, а затем переходите на свой сайт в методе testEasy(). Должно быть наоборот.

+0

Благодарим вас за ответ. Я обновляю свой файл и редактирую сообщение, но он все тот же. – Djoh

+0

Можете ли вы распечатать содержимое заголовка переменной, что получает webdriver – Grasshopper

+0

Благодарю вас за ответ. Я пытаюсь вернуться (название), но я не уверен, где мне нужно поставить эту команду. Можете ли вы зажечь меня, пожалуйста? – Djoh

Смежные вопросы