1

I got an error with my maven test in localhost. One day ago everything was fine. I test the title on my home page if it contains a word. If contains the same word return true else false but each time there is a failure even if my title is good. It's weird because I didn't change anything.

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();
    }
}   

And then the beginning of my file to test :

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

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

Here is the result of my build :

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


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

Is there someone can help me please ?

abarisone
  • 3,707
  • 11
  • 35
  • 54
Djoh
  • 27
  • 3
  • Your code contains _Assert.assertTrue(title.contains("TimDevops"));_ while the title is _Bienvenue sur TimDevOps_. Please check this as the Strings should be equal. – JDelorean Aug 23 '16 at 11:13
  • Also, I understand this is a local webpage, nevertheless you should implement some kind of `waitFor()` method after `driver.open()`. Otherwise an exception will be thrown before Selenium can open the browser/load the page. – JDelorean Aug 23 '16 at 12:00
  • @JDelorean thank you for your answer. I don't know how can I put the method waitFor(). – Djoh Aug 23 '16 at 12:46
  • Try to add some waiter. You can wait visibility of some web element on page and then check the title. Maybe page wasn't loaded when you make your check. – Anton Savostytskyi Aug 23 '16 at 12:53
  • Could you add getPageSource() output after you loaded page? – unickq Aug 23 '16 at 13:12
  • @Djoh [Here](http://stackoverflow.com/questions/11736027/webdriver-wait-for-element-using-java) is how you can implement `waitFor()` method. – JDelorean Aug 23 '16 at 13:18
  • @JDelorean thank you for your answer. I add a try catch with driver.wait() but there is a blank page when the firefox is launched. – Djoh Aug 23 '16 at 14:43

1 Answers1

0

You are first getting the title and then navigating to your site in the method testEasy(). Should be the reverse.

Grasshopper
  • 8,908
  • 2
  • 17
  • 32
  • Thank you for your answer. I update my file and edit the post but it's still the same – Djoh Aug 23 '16 at 10:08
  • Can you print out the contents of the variable title what webdriver is getting for you – Grasshopper Aug 23 '16 at 10:10
  • thank you for your answer. I try return(title) but I'm not sure where I have to put this command. Can you light me please ? – Djoh Aug 23 '16 at 12:43
  • @Djoh he is asking you to add System.out.println(title) before Assert statement – Mrunal Gosar Aug 23 '16 at 13:34
  • @MrunalGosar Thank you for your answer. Yes I did it but there is nothing in the console. I'm sorry I'm not an expert in Java. – Djoh Aug 23 '16 at 14:42
  • If there's nothing on the console means there is nothing in the title..so either using webdriverwait and expectedconditions you wait for the presence of expected title or you ask your dev to add some title (if its missing) – Mrunal Gosar Aug 24 '16 at 06:14
  • @Djoh When you run this test do you see your page being loaded on your browser or you getting a blank screen? If you are getting a blank screen you may have setup issues with firefox. Find out which version of firefox browser you are using as there has been changes to how selenium webdriver interacts with latest version of firefox browsers. Have you updated your firefox to latest version? – Grasshopper Aug 24 '16 at 06:21
  • Yes when I run my test I see my page is being loaded on my browser. When I put a driver.wait() I see the page is loading then the page disappear and I get a new Firefox blank page. Yes it's a latest version of Firefox. I use the version 47.0.1 and my test was good few days ago with this configuration – Djoh Aug 24 '16 at 08:47