selenium basics

Upload: saikrishnatadiboyina

Post on 08-Jan-2016

215 views

Category:

Documents


0 download

DESCRIPTION

selenium

TRANSCRIPT

  • 7/17/2019 Selenium Basics

    1/4

    Step 3: Compile the .au3 script and convert it in to .exe file

    Now save the above script, if in case you have not saved the name earlier, please save it in .au3 format. The next step is to convert it in to .exe format. Forthat you need to right click on the .au3 file and select Compile Script.Step 4: Call the .exe file in to the Selenium test case

    Once you done with the compiling, it will create the .exefile with the same namer the same folder and that .exefile will be called in the Selenium Test Script bng the following script:Runtime.getRuntime().exec("D:\AutoIt\AutoItTest.exe");

    Runtime.getRuntime().exec("D:\AutoIt\AutoItTest.exe");

    Need of third party tools in Selenium

    Web applications do not always confine themselves to working entirely on the web. Sometimes they need to interact with the desktop to do things like downloads &uploads. Automating these sorts of workflow is tricky in Selenium. Selenium isconfined to automating browsers, so desktop windows are out of scope. If you arelooking to automate workflows that go from browser to desktop and back in Selenium, then a little AutoIt is in order.What is AutoIt

    AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks ina way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying runtimesrequired!

    In laymans term AutoIt is just another automation tool like Selenium but unlike Selenium it is used for Desktop Automation rather Web Automation. It is a powerfultool and it just not automate desktop windows, button & form, it automates mouse movements & keystrokes too. Just like Selenium IDE, it also gives you the recording capability which generates the scripts for you to use the same script in y

    ou test.

    Could any one guide how to integrate an Auto IT script with in selenium web driver?

    I am using a applet window embedded with in browser.So, using Auto IT for keystroke & Mouse action.

    I don't want to run the Auto It script as an .exe file in selenium.

    I just want to covert the script in to selenium and run for future enhancement.

    Answer:1 down vote accepted

    The solution: AutoITx.jar. import the jar in selenium and directly use the autoit script in selenium webdriver

  • 7/17/2019 Selenium Basics

    2/4

    ScreenShot:File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //The below method will save the screen shot in d drive with name "screenshot.png" FileUtils.copyFile(scrFile, new File("D:\\screenshot.png"));

    WebDriver driver = new FirefoxDriver();driver.get("http://www.google.com/");File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);// Now you can do whatever you need to do with it, for example copy somewhereFileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

    ImplicitlyWait Command

    Purpose: This means that we can tell Selenium that we would like it to wait fora certain amount of time before throwing an exception that it cannot find the element on the page. We should note that implicit waits will be in place for theentire time the browser is open. This means that any search for elements on thepage could take the time the implicit wait is set for.FluentWait Command

    Implicit waits are basically your way of telling WebDriver the latency that youwant to see if specified web element is not present that WebDriver looking for.So in this case, you are telling WebDriver that it should wait 10 seconds in cases of specified element not available on the UI (DOM).

    Implicit wait time is applied to all elements in your script

    Purpose: Each FluentWait instance defines the maximum amount of time to wait fora condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

    Explicit waits are intelligent waits that are confined to a particular web element. Using explicit waits you are basically telling WebDriver at the max it is to wait for X units of time before it gives up.

    Explicit wait time is applied only for particular specified element. In Explicit you can configure, how frequently (instead of 2 seconds) you want to check condition

    Explicit wait

    public static WebElement explicitWait(WebDriver driver,By by){

    WebDriverWait wait = new WebDriverWait(driver, 30);

    wait.until(ExpectedConditions.presenceOfElementLocated(by));

    Synchronization in Selenium Webdriver

    It is a mechanism which involves more than one components to work parallel withEach other.

  • 7/17/2019 Selenium Basics

    3/4

    Generally in Test Automation, we have two components1. Application Under Test2. Test Automation Tool.

    Both these components will have their own speed. We should write our scripts insuch a way that both the components should move with same and desired speed, sothat we will not encounter "Element Not Found" errors which will consume time again in debugging.

    Synchronization can be classified into two categories:

    1. Unconditional2. Conditional Synchronization

    Unconditional :In this we just specify timeout value only. We will make the tool to wait untilcertain amount of time and then proceed further.

    Examples: Wait() and Thread.Sleep();

    The main disadvantage for the above statements are, there is a chance of unnecessary waiting time even though the application is ready.

    The advantages are like in a situation where we interact for third party systemslike interfaces, it is not possible to write a condition or check for a condition. Here in this situations, we have to make the application to wait for certainamount of time by specifying the timeout value.

    Conditional Synchronization:

    We specify a condition along with timeout value, so that tool waits to check forthe condition and then come out if nothing happens.

    It is very important to set the timeout value in conditional synchronization, because the tool should proceed further instead of making the tool to wait for a particular condition to satisfy.

    In Selenium we have implicit Wait and Explicit Wait conditional statements. Check here for Examples on how to use Webdriver Waits

    1. Implicit Wait.

    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 when we define the implicit wait, it will set forthe life of the WebDriver object instance.

    It is a mechanism which will be written once and applied for entire session automatically. It should be applied immediately once we initiate the Webdriver.

    Implicit wait will not work all the commands/statements in the application. It will work only for "FindElement" and "FindElements" statements.

    If we set implicit wait, find element will not throw an exception if the elementis not found in first instance, instead it will poll for the element until thetimeout and then proceeds further. We should always remember to add the below syntax immediately below the Webdriver statement.

  • 7/17/2019 Selenium Basics

    4/4

    Syntax: driver.manage.TimeOuts.implicitwait(6,Timeunit.SECONDS);

    Example:WebDriver driver = new FirefoxDriver();driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);driver.get("www.google.com");

    Explicit Wait:

    We need to define a wait statement for certain condition to be satisfied until the specified timeout period. If the Webdriver finds the element within the timeout period the code will get executed.

    Explicit wait is mostly used when we need to Wait for a specific content/attribute change after performing any action, like when application gives AJAX call tosystem and get dynamic data and render on UI.

    Example: Like there are drop-downs Country and State, based on the country valueselected, the values in the state drop-down will change, which will take few seconds of time to get the data based on user selection.

    Example:

    /*Explicit wait for state dropdown field*/ WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("statedropdown")));

    Fluent Wait:

    Using FluentWait we can define the maximum amount of time to wait for a condition, as well as the frequency with which to check for the condition.

    And also the user can configure to ignore specific types of exceptions such as "NoSuchElementExceptions" when searching for an element.

    Syntax:

    Wait wait = new FluentWait(driver)

    //Wait for the condition .withTimeout(30, TimeUnit.SECONDS)

    // which to check for the condition with interval of 5 seconds.

    .pollingEvery(5, TimeUnit.SECONDS)//Which will ignore the NoSuchElementExcept

    ion .ignoring(NoSuchElementException.class);

    }