Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert command for checking the url in selenium webdriver

As there is no xpath and id for the url on the webpage, how can i check if my actual url is matching with the expected url?

I have provided my code below, but it did not work.

String Actualtext = driver.findElement(By.linkText("http://localhost:8080/imdb/homepage")).getText();
Assert.assertEquals(Actualtext, "http://localhost:8080/imdb/homepage" );
System.out.println("URL matching --> Part executed");
like image 346
Min Avatar asked Jan 05 '15 08:01

Min


2 Answers

You can validate it against the 'current url' as

String URL = driver.getCurrentUrl();
Assert.assertEquals(URL, "http://localhost:8080/imdb/homepage" );
like image 70
nitin chawda Avatar answered Sep 20 '22 12:09

nitin chawda


getText() is used to fetch the visible innertText, You can use the getAttribute method to fetch the url (for hyperlink), something like below

String Actualtext = driver.findElement("YourElementLocator").getAttribute("href")
Assert.assertEquals(Actualtext, "http://localhost:8080/imdb/homepage" );
System.out.println("URL matching --> Part executed");
like image 44
Paras Avatar answered Sep 20 '22 12:09

Paras