Reputation: 92
I am trying to run a simple automated test script to get webpage title and verify it by using in selenium-webdriver by Java and IE, but getTitle() method returns strange result.
I could see when I run my test script initially is getting load a default page with below URL,
and then its navigating to my target URL (which is mentioned in script).
But still script is capturing the page title from above localhost URL not from target URL. Please advise how can I bypass the default (http://localhost:44585/) page.
Script
public class start_with {
public static void main (String [] args)
{
String url ="http://google.com";
String title = "Google";
String Actual_title;
System.setProperty("webdriver.ie.driver", "C:\\..\\IEDriverServer.exe");
WebDriver my_browser;
my_browser = new InternetExplorerDriver();
my_browser.get(url);
Actual_title = my_browser.getTitle();
if ( Actual_title.contentEquals(title))
System.out.println(" Test Passed");
else
System.out.println(" Test Failed \n Title is "+ Actual_title);
my_browser.close();
}
}
Upvotes: 0
Views: 1883
Reputation: 193088
A few words about your script-
With Selenium 3.4.0, IEDriverServer 3.4.0 & IE 10.0.x I don't see any issue within your code but a few points here:
Load a default page http://localhost:44585/
- Yes, that's how IEDriverServer works. We can't help it.
WebDriver my_browser;
- As a good programming practice you should keep your code easily understandable for reviews & peer reviews. Hence instance of WebDriver
should be driver
or something in similar lines.
String title = "Google";
- Here the name of the variable should have been Expected_title
which would have been in similar lines of String Actual_title;
Actual_title.contentEquals(title)
- You have used if ( Actual_title.contentEquals(title))
as you have observed the Page Title as only "Google". But in most cases you won't be able to see the entire Page Title simply by viewing it. You have to see the page source to observe the entire Page Title. So in your case instead of Actual_title.contentEquals(title)
you can use if ( Actual_title.contains(title))
so your Testcase doesn't fails.
If you find your code failing while trying to locate elements before the page being loaded, add implicitlyWait to get the page loaded.
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
Upvotes: 2
Reputation: 945
It's probably timing issue, you are fetching title too soon, before new page is fully loaded. For test, try to put some sleep and get title after that. So in your code it would look like this:
my_browser.get(url);
Thread.sleep(15000);
Actual_title = my_browser.getTitle();
If it works then remove this nasty Thread.sleep()
and put some better waiter.
Upvotes: 1
Reputation: 910
May be you need to set an appropriate pageLoadTimeout for Webdriver. Before executing get(); method for opening the required url, you can set the pageloadtimeout in following way:
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
Let me know, if it resolves your issue.
Upvotes: -1