Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to change the URL text using Selenium Webdriver?

Let's suppose that I am writing Selenium tests for some public library website. The website has a login page. After logging into the website, I see a list of books. I can click on any of the links of the book titles. After clicking on a book title link, a summary of the book is dispalyed on the screen, and the url is something like:

http://somePublicLibrary.com/Books/e6a9bb54-da25-102b-9a03-2db401e887ec?title=BookTitle

If I change the guid in the url to some invalid guid, then instead of displaying the summary, the page displays a message:

No summary found.

I would like to create an automated test for this scenario.

I am trying to write methods like changeUrlGuid(), getUrlGuid(), etc. But how can I get the url text using Selenium Webdriver?

like image 765
Maya Avatar asked Oct 18 '12 21:10

Maya


Video Answer


2 Answers

You can get the current url of the page loaded by using getCurrentUrl() method.

String url = webdriver.getCurrentUrl();

Then, all you have to do is replace old guid with a new guid.

int numOfChars = 36;
int posOfQuestionMark = url.indexOf("?");
String newGuid = "..."; // put new/wrong guid value here
String newUrl = url.substring(0, posOfQuestionMark-numOfChars)+newGuid+url.substring(posOfQuestionMark);

--Edit--

Now, load this new url in the browser.

webdriver.get(newUrl);
like image 190
rizalp1 Avatar answered Dec 15 '22 04:12

rizalp1


Try the following:

 driver.current_url
like image 26
Nat Ritmeyer Avatar answered Dec 15 '22 03:12

Nat Ritmeyer