Selenium Guideline for Beginners - Part 3



  • I. SVN/GIT ignores

    As you see when you run your project with mvn install command, or import your project into Eclipse with mvn eclipse:eclipse command, there will be created files and folders that are localized to your individual setup or running.
    When working in a shared project using SCM (source code management) tool such as SVN or GIT, it is your responsibility to avoid to commit such individual stuffs.
    This can be automatically prevented by setting the tool to ignore (unversion) specified files and folders.
    In GIT, it is very easily done by configuring .gitignore file.

    1. In the project root folder, just create a file name .gitignore and list all the files and folders line by line. Here is a common gitignore file for Java/Maven/Eclipse projects that you can re-use:

       *.class
       # Package Files #
       *.jar
       *.war
       *.ear
       # Others #
       .settings
       .project
       .classpath
       .idea
       *.iml
       *.ipr
       *.iws
       temp
       bin
       target
       pom.xml.versionsBackup
       .svn
       *~
       .DS_Store
       transaction.log
      
    2. Commit this file.
      In SVN, it is quite awkward that you cannot get it done at once for all. As TortoiseSVN document suggests, you have to wait till a would-be-ignored file/folder appears to add it to ignore list.

    II. Selenium

    2.1 What is Selenium?

    Selenium, by SeleniumHQ.org, is a framework for automating web browsers. In web testing, it is used to simulate user actions. Some facts about Selenium:

    • Selenium interacts with web browsers. To work with OS controls like upload/download dialog, you need to use other frameworks like Java AWT which can simulate key and mouse.

    • Beside Java, Selenium API supports several languages.

    • Selenium fits UI testing and is widely used for regression. In its design, Selenium has nothing to do with tests that require concurrent users.

    • We are actually talking about Selenium 2, aka Selenium WebDriver. Selenium 1, refered as Selenium IDE at its era, was a Firefox addon to record and playback user actions.

    • Like other robots, Selenium is blocked by captcha ^^

    • FirefoxWebDriver, unlike other WebDriver, works with Firefox via a Firefox addon. Other WebDriver requires an ex-ternal binary to work with browsers. FirefoxWebDriver no longer supports Firefox newer than 45. MarionetteWebDriv-er is the substitute.

    If you have time, read an introduction by SeleniumHQ at http://www.seleniumhq.org/docs/03_webdriver.jsp
    In this session, you write a simple Selenium test. The test starts a Firefox session, goes to google.com and searches for a keyword.
    We start with FirefoxWebDriver because it does not require a binary, and continue to use Firefox for future development because of its familiar addons - FireBug/FirePath. However, for a while you have to stick with Firefox 45.

    1. Check your Firefox version. If it is newer than 45, uninstall it and install Firefox 45.
    2. Disable Firefox Auto Update
    3. Manually test to make sure there is no connectivity problem: go to google.com and search something.
    4. Now stay at google.com, right click at the search box and select Inspect Element. For a moment you can use the built-in Inspector but for the future, you should install FireBug/FirePath.
    5. Look at the highlighted element:

    Here you see an input tag with type=text, id=lst-ib, name=q. It is located inside a form tag with id=tsf. Those infor-mation will be picked up to create a "locator" in your code.

    1. Enough with the manual test. Now start your coding in your Eclipse project:
      Edit Test1.java:

       package com.gnc.csm;
       import java.util.concurrent.TimeUnit;
       import
       import
       import
       import
       import
       import
       import
       import
       org.openqa.selenium.By;
       org.openqa.selenium.Keys;
       org.openqa.selenium.WebDriver;
       org.openqa.selenium.WebElement;
       org.openqa.selenium.firefox.FirefoxDriver;
       org.testng.annotations.AfterTest;
       org.testng.annotations.BeforeTest;
       org.testng.annotations.Test;
       public class Test1 {
       protected WebDriver driver;
       public void init() {
       driver = new FirefoxDriver();
       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       }
       @Test
       public void test() throws InterruptedException {
       //goto Google
       driver.navigate().to("http://google.com");
       //find the input
       WebElement input = driver.findElement(By.id("lst-ib"));
       //enter a search
       input.sendKeys("selenium tutorial");
       input.sendKeys(Keys.ENTER);
       //pause 5s before the browser closes, so that you can see the result ^^
       Thread.sleep(5000);
       }
       @AfterTest
       public void teardown() {
       if (driver != null) {
       driver.quit();
               }
           }
       }
      
    2. Run your test in Eclipse: while Test1.java is opened and takes focus, from Eclipse menu choose Run → Run
      If it works, it should open Firefox and do the search.

    2.2 Locator

    Focus on this line:

        WebElement input = driver.findElement(By.id("lst-ib"));
    

    Here you use a By object to find the input. The By object is called a locator.
    As inspected, the input has some possible locators:

    • #1 : By.id("lst-ib"), because it has an id.

    • #2 : By.name("q"), because it has a name.

    • #3 : By.tagName("input"), because it is an input tag.

    • #4 : By.className("gsfi"), because it has a class name.

    At a glance, you will kick out the #3 and #4, because they are too common, the page probably has many elements that share the same tag name and class. #1 and #2 look fair enough, you can pick them but of course you need to test.
    It is preferable to use By.xpath, because xpath can combine many conditions of an element. Here are some examples:

    • #5 : By.xpath("//input[@id='lst-ib']"), expresses an input tag having id=lst-ib

    • #6 : By.xpath("//input[@name='q']"), an input having name=q

    • #7 : By.xpath("//input[@class='gsfi']"), an input having class=gsfi

    • #8 : By.xpath("//input[@class='gsfi' AND @name='q']"), a combination of three conditions.

    Like the name suggests, XPath always expresses a path. The following example adds an condition that the input is in a form element:

    • #9 : By.xpath("//form//input[@id='lst-ib']")

    Another reason to use XPath is that you can test it using FirePath. In XPath box, enter your expression:
    Nguồn: Viblo


Hãy đăng nhập để trả lời
 

Có vẻ như bạn đã mất kết nối tới LaptrinhX, vui lòng đợi một lúc để chúng tôi thử kết nối lại.