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?
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);
Try the following:
driver.current_url
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With