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