Reputation: 13
I have written some code but it works only until it gets the webpage title. When the title is verifying the code is giving test failed.
The scenario is as follows:
Code is :
package sample;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class search_demo {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver","C:\\Users\\HP\\Downloads\\geckodriver-v0.26.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.google.com/");
driver.manage().window().maximize();
// Click on the search text box and send value
WebElement element= driver.findElement(By.name("q"));
element.sendKeys("Nishtha tiwari");
element.submit();
WebDriverWait wait = new WebDriverWait(driver,20);
WebElement elem= wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#rso > div:nth-child(3) > div > div:nth-child(1) > div > div > div.r > a")));
elem.click();
driver.navigate().to("https://in.linkedin.com/in/nishtha-tiwari-40a95281");
Thread.sleep(2000);
String title =driver.getTitle();
System.out.println("Page title is:" + title);
String expectedTitle = "LinkedIn: Log In or Sign Up ";
if (title.equals(expectedTitle))
System.out.println("Test Passed!");
else System.out.println("Test Failed");
driver.close();
}
}
Upvotes: 1
Views: 1109
Reputation: 33384
When I looked at your code and checked page source of Linked In page
I have found the title with ends
That is reason it is always getting failed.
Solutions:
So to get rid of that you have two options.
Option 1: Remove all
from string.
String title =driver.getTitle();
title = title.replaceAll("&"+"nbsp;", " ");
title = title.replaceAll(String.valueOf((char) 160), " ");
System.out.println("Page title is:" + title);
String expectedTitle = "LinkedIn: Log In or Sign Up";
if (title.trim().equalsIgnoreCase(expectedTitle))
{
System.out.println("Test Passed!");
}
else
{
System.out.println("Test Failed");
}
Option 2: Use contains instead.
String title =driver.getTitle();
System.out.println("Page title is:" + title);
String expectedTitle = "LinkedIn: Log In or Sign Up";
if (title.contains(expectedTitle))
{
System.out.println("Test Passed!");
}
else
{
System.out.println("Test Failed");
}
Upvotes: 2