Thursday, 21 June 2018

Waiting the page move to other page to check the URL

When I was doing an automation testing, there is a situation that two page has an almost same element and layout, and might be the Xpath and title are the same, so how to make sure that the page has already changed.

In my opinion, I'd like to use the percent of keywords of  URL to make sure that the page has changed.

Such as I am now at the PropertiesOwnerPage

Then at the search box, I enter the "Morgan House" and then click the search button
All the layout are almost same and it is now not easy to make sure that the page is really moved to the search result Page. So I use way to figure it out.

            BtnSearch.Click();
            while (!_driver.Url.Contains("SearchString"))
            {
                Thread.Sleep(200);
            }
            return new SearchResultPage();

that means then the BtnSearch clicked, until the page goto the search result page, or it will wait for 200ms and then check the if URL  has changed. if it has changed, then move to SearchResultPage

Wednesday, 20 June 2018

Selenium with C# (Waiting)

Selenium with C# (Waiting)
 0.cs
webdriver.FindElement(By.Id("search_button")).Click();
Thread.Sleep(5000);
IWebElement searchResult = webdriver.FindElement(By.Id("search_result"));
 1.cs
IWebDriver webdriver = new ChromeDriver();
webdriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
webdriver.FindElement(By.Id("search_button")).Click();
IWebElement searchResult = webdriver.FindElement(By.Id("search_result"));
 2.cs
WebDriverWait webdriverWait = new WebDriverWait(webdriver, TimeSpan.FromSeconds(5));
IWebElement searchResult = webdriverWait.Until(x=>x.FindElement(By.Id("search_result")));
 3.cs
DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(webdriver);
fluentWait.Timeout = TimeSpan.FromSeconds(5);
fluentWait.PollingInterval = TimeSpan.FromMilliseconds(250);
fluentWait.IgnoreExceptionTypes(typeof(NoSuchElementException));
IWebElement searchResult = fluentWait.Until(x=>x.FindElement(By.Id("search_result")));

Saturday, 16 June 2018

DDT with Excel in C# and Selenium

there are some limitation while using DataSource attribute like
  • We don’t have control over the iteration (via code, but we can do via properties)
  • We cannot be very precise about which column and row data we need

Custom data driven library for excel

Hence to overcome to shortcoming of DataSource attribute, we are going to design our own custom data driven library for excel, As always, before starting to create any custom library, it’s always a good practice to first create a design before writing code. Our design looks something like this

Reading and parsing data from excel

For reading and parsing data from excel, instead of we do lot of code to read data out from excel (since excel libraries are exposed as COM components and hence reading them need COM interops library) we will use one of the most popular excel data reader available in codeplex called Excel Data Reader
You can also download via Nuget package manager to reference in your project.
Here is the code snippet for the above video
we discussed how to
  • Design our custom library,
  • Reading and parsing data using ExcelDataReader and
  • Converting the data into Data Table.
In this part, we will discuss storing the data into a collection and then read data for our test method from the collection.

Why use Collection?

Well, reading data just once from external data source like Excel sheet and storing the data in-memory like collections, will improve the performance tremendously as opposed to reading data each and every time from external data source like excel, since this will affect the performance of our test script.

Storing data in C# collections

We will store all the data from Data Table retrieved via ExcelDataReader into C# collection, for this we will do the following steps
  • Create a Custom class
  • Create a List<CustomClass>
  • Populate the ExcelDataReader data into List<CustomClass> to our required format

Custom Class

Our custom class will have following auto-implemented properties
  • RowNumber – hold all the row numbers
  • ColumnName – holds column name
  • ColumnValue – holds column value
  • Custom Class
    Populating Data into Collections
    Reading data from Collection

What to do to make sure HTML Table is loaded completely if implicit and explicit wait are not working.

These days I was working on a project, to implement the delete function of a listing. Such as below: Actually,  every time deleted the it...