2

First timer here to Stackoverflow, and a beginner to Java/Scripting/Selenium/TestNG

I created a simple script to check page meta data which correctly prints pass or fail if expected page title, I modified some code from a tutorial I found. I Later tried to add the test of some TestNG frame work modding the code a bit more, but even though say the test can fail in my print output the TestNG output fails

Here is my code thus far

package live_MetaData;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;



public class Demo{
         private WebDriver driver;
         private StringBuffer verificationErrors = new StringBuffer();

         @BeforeClass(alwaysRun = true)
         public void setUp() throws Exception {
          System.setProperty("webdriver.gecko.driver", "C:\\Automation\\SeleniumFiles\\Browser Drivers\\geckodriver.exe");
          driver = new FirefoxDriver();
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        }

        @Test
        public void DemoTest() throws Exception {
        String baseUrl = "http://demo.guru99.com/test/newtours/";
        String expectedTitle = "Not Meta";
        String actualTitle = "";

        driver.get(baseUrl);

        actualTitle = driver.getTitle();

        if (actualTitle.contentEquals(expectedTitle)){
            System.out.println("Test Passed!");
        } else {
            System.out.println("Test Failed");}}

        //close Fire fox
        @AfterClass(alwaysRun = true)
        public void tearDown() throws Exception {
          driver.quit();
          String verificationErrorString = verificationErrors.toString();
          if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);

    }

}}

Which gives me this output text:

Test Failed

PASSED: DemoTest

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Thanks for any help especially if it could be explained where Im going wrong and why it should be like in (hopefully) Solution, I went from not knowing anything on Tuesday to this.

  • This is correct ! actualPageTitle is "Welcome: Mercury Tours" and expectedTitle = "Not Meta" , else case would be executed. – cruisepandey Apr 13 '18 at 09:40
  • @cruisepandey As I said the test it self works and shows it as failed hence the "Test Failed" part of the output, but the output from TestNG shows it as passed, this is the bit I;m trying to fix and understand – Leon Tilbrook Apr 13 '18 at 09:43
  • @LeonTilbrook You are trying to fail the test in the afterclass with the fail() method. The flow does not enter the 'if' condition because the verificationErrorString variable is empty string. You need to modify the code to add some error to the verificationErrors stringbuffer where you initially compare titles. Then it will enter the if condition and fail the test. – Grasshopper Apr 13 '18 at 09:55

1 Answers1

-1

Your actualTitle is "Welcome: Mercury Tours" and expectedTitle = "Not Meta"

Your code :

if (actualTitle.contentEquals(expectedTitle)){
            System.out.println("Test Passed!");
        } else {
            System.out.println("Test Failed");}

Control of problem will go to else , since if condition didn't meet. Your Test case is pass but you are printing "Test Failed" in else.

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

This means that Test case pass count is 1.
Test case failure count is 0.
Test case skip count is 0.

My suggestion : Use Assertions to verify this kind of conditions.

Use this code for assertions :

@Test
        public void DemoTest() throws Exception {
        String baseUrl = "http://demo.guru99.com/test/newtours/";
        String expectedTitle = "Not Meta";
        String actualTitle = "";
        driver.get(baseUrl);
        actualTitle = driver.getTitle();
        assertEquals(actualTitle , expectedTitle );

}

cruisepandey
  • 28,520
  • 6
  • 20
  • 38