FenWangNZ / blog

I love learning. This is my wiki.
0 stars 0 forks source link

Selenium: Assert current page stays where it was after cancelling #2

Open FenWangNZ opened 3 years ago

FenWangNZ commented 3 years ago

How to assert after canceling unsaved changes current page stays where it was?

Introduction

I don't think this is the best way to do the assertion... however, I currently have no idea of other options. Welcome any comments. Thanks in advance.

Tasks

Today, I tested canceling unsaved changes function for new invoice creation. I designed two test scenarios for this cancelling.

Task1 and Yes in Task2 are similar. I only need to verify a button is available on the previous page. But I really have no idea about find elements to verify the current page didn't change. I actually thought I should verify all the element on this page, but I don't know how to read the text value that I input into the field....so seems a lot is going on. So I am going to talk about how to assert current page stays where it was in task2. (Maybe someday I can get to know another way.)

The solution is to use this bit.

IWebDriver driver = new Chromedriver(); 
String currentURL = driver.Url; 

So this part of the code is:

//Go to invoice creation page. Do some changes, then click on the cancel button -> click no button.
driver.FindElements(By.XPath("//*[@class='btn btn-lg btn-success']"))[0].Click();
Thread.Sleep(2000);
driver.FindElement(By.XPath("//*[@name='clientname']")).Clear();
driver.FindElement(By.XPath("//*[@name='clientname']")).SendKeys("abc");
Thread.Sleep(2000);
string url_0 = driver.Url; //Get URL before modal pops up
driver.FindElements(By.XPath("//*[@class='btn btn-secondary']"))[0].Click();
Thread.Sleep(5000);
//click on no, then stay where it was.
driver.FindElement(By.XPath("//*[@class='btn btn-primary']")).Click();
Assert.AreEqual(url_0, driver.Url); //Get current URL
//Thread.Sleep(1000);

Thanks and comments.