Test flow control

TABLE OF CONTENTS

  • Explicit waits
  • Implicit waits
  • Popup and alert windows
  • Frames
  • Verifications and Asseritons

Explicit waits

    Selenium Waits
  • Waiting is having the automated task execution elapse a certain amount of time before continuing with the next step.
  • WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10s and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds

Explicit waits

  • An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code.
  • The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait is one way this can be accomplished.

Explicit waits (2)

The code below waits up to 60 seconds and if it finds the element will return it in 0 - 60 seconds. By default we call the check every 500 milliseconds until it returns successfully.

Waits for 60 seconds
IWebDriver driver = new FirefoxDriver();
driver.Navigate()
  .GoToUrl("http://foo.com/slow-page/");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
Func<IWebDriver, bool> waitForElement =
   new Func<IWebDriver, bool>((IWebDriver Web) =>
   {
       Console.WriteLine(Web.FindElement(By.Id("target"))
        .GetAttribute("innerHTML"));
       return true;
   });
wait.Until(waitForElement);
						

Implicit waits

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

Waits for 45 seconds
WebDriver driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(45));
driver.FindElement(By.Id("foo")).Click();
						

Custom-expected conditions (JAVA)

    Expected conditions
  • There are some common conditions that are frequently come across when automating web browsers. Java happens to have convenience methods so you don’t have to code an ExpectedCondition class yourself or create your own utility package for them.
  • e.g. Element is Clickable => it is Displayed and Enabled
  • The ExpectedConditions package for Java contains a set of predefined conditions to use with WebDriverWait.
Waits for 10 seconds
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element =
	wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
						

Expected conditions (2)

  • alertIsPresent() : Is Alert Present?
  • elementSelectionStateToBe: Is the element selected?
  • elementToBeClickable: Is the element clickable?
  • elementToBeSelected: Element is selected
  • frameToBeAvailableAndSwitchToIt: Is frame available and selected?
  • invisibilityOfElementLocated: Is the element invisible?

Expected conditions (3)

  • presenceOfAllElementsLocatedBy: All elements presence
  • location.refreshed: Wait for a page refresh.
  • textToBePresentInElement: Is the text present for a particular element?
  • textToBePresentInElementValue: Is the element value present for a particular element?
  • visibilityOf: Is the element visible?
  • titleContains: Is that title contain?

Popup and alert windows

    Popup and alert windows
  • There are two types of alerts that we would be focusing on majorly:
    • Windows based alert pop ups
    • Web based alert pop ups
  • Handling pop up is one of the most challenging piece of work to automate while testing web applications. Owing to the diversity in types of pop ups complexes the situation even more.
    Popup and alert windows (2)
  • Web Based Popups
  • We can handle the alerts very easily in Selenium WebDriver in C# using SwitchTo() method.
    • void Dismiss() – The dismiss() method clicks on the “Cancel” button as soon as the pop up window appears.
    • void Accept() – The accept() method clicks on the “Ok” button as soon as the pop up window appears.
    • string Text() – The getText() method returns the text displayed on the alert box.
Popup and alert windows
IWebDriver driver=null;
try
{
   driver = new ChromeDriver(@"F:\selenium\csharp");
   driver.Url = "http://www.google.co.in";
   driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20));
   driver.Navigate();
   driver.FindElement(By.Id("link")).Click();
   // click on ok button of alert
   driver.SwitchTo().Alert().Accept();
}
catch(Exception e){
   Console.WriteLine("Exception ******" + e.ToString());
}
finally{
   Thread.Sleep(2000);
   driver.Quit();
   Console.ReadLine();
}
					
    Popup and alert windows (3)
  • Sometimes we need to handle pop ups generated by Windows like a print pop up or a browsing window while uploading a file.
  • Handling these pop-ups have always been a little tricky as we know Selenium is an automation testing tool which supports only web application testing, that means, it doesn’t support Windows based applications and window alert is one of them. However Selenium alone can’t help the situation but along with some third party intervention, this problem can be overcome.
    Popup and alert windows (4)
  • There are several third party tools available for handling window based pop-ups along with the selenium.
  • We can handle a window based pop up using AutoIt.
  • AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting.
    Popup and alert windows (5)
  • Automation with AutoIt
AutoIt
public static void HandlePrintDialogue()
{
    au3.WinWait(AiWindows.PrintWindow, "", 10);
    au3.WinActivate(AiWindows.PrintWindow);
    au3.Send(AiKeystrokes.Enter);
}
						

Frames

Frames (2)

Moving Between Windows and Frames - some web applications have many frames or multiple windows. WebDriver supports moving between named windows using the “switchTo” method:

switchTo
driver.SwitchTo().Window(driver.WindowHandles.Last());
						

Frames (3)

All calls to driver will now be interpreted as being directed to the particular window. But how do you know the window’s name? Take a look at the javascript or link that opened it:

windowName
Click here to open a new window
						

Frames (4)

Alternatively, you can pass a “window handle” to the “switchTo().window()” method. Knowing this, it’s possible to iterate over every open window like so:

SwitchTo window
foreach (string handle in driver.WindowHandles)
{
    driver.SwitchTo().Window(handle);
}
						
You can also switch from frame to frame (or into iframes):
SwitchTo window
driver.SwitchTo()
  .Frame(driver.FindElement(By.Xpath("//frame[@src='/somesrc' and not(@id='unloadFrame')]")));
						

Verifications and Asseritons

    Verifications and Asseritons (2)
    • Assert command in Selenium:
    • When an "assert" command fails then test execution will be aborted.
    • Assert is best used when the check value has to pass for the test to be able to continue to run. Like a log in.
    • Verify command in Selenium:
    • When a "verify" command fails then test will continue executing and logging the failure.
    • Verify is best used to check non critical things. Like the presence of a headline element.
    Verifications and Asseritons (3)
  • NUnit assertion - Assert.AreEqual(actual, expected)
  • This assertion is useful to compare expected and actual values in selenium webdriver. If both values match then its fine and will continue execution. But if fails then immediately it will mark that specific test method as fail and exit from that test method.
  • You can use different types of values in actual and expected like boolean, byte[], char, double, float, int, etc.. but function is same for all. Let we look at simple example to understand it better.
Assert.AreEqual
Assert.AreEqual(actualtext, "Tuesday, 28 January 2014");
						
office@e-dojo.it.com

EXERCISE

Problem 1: Implicit waits

Open a web site of your choice and implement at least 2 tests using only Implicit wait.

Problem 2: Explicit waits

Open the same web site and refactor your tests using only Explicit wait.

Problem 3: Popup and alerts

Open http://www.mobile.bg/, perform a search and try to assert the (pictures) results are found.

EXERCISE (2)

Problem 4: File dropper

Description Open http://www.filedropper.com/ and upload a file using the web s ite.

Problem 5: Floating Ball

Open http://www.littlewebhut.com/php/samples.php?page=xhtml_article_iframe#codetester and select the Floating ball option.