Maya
Maya

Reputation: 7223

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?

Upvotes: 6

Views: 13667

Answers (2)

rizalp1
rizalp1

Reputation: 6534

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

Upvotes: 8

Nat Ritmeyer
Nat Ritmeyer

Reputation: 5678

Try the following:

 driver.current_url

Upvotes: 3

Related Questions