How to use Firefox webdriver to launch Firefox browser automatically in java

Hello everybody, today i will show you how to write basic test automation script using selenium webdriver.
You will learn how to use Firefox webdriver to initiate a Firefox browser and implement basic browse page actions with selenium.

Before start to learn, you had better read following articles if you need.

After read all above articles, you can begin to write your first test automation script using selenium webdriver.

Sample test case steps and codes

  1.  Open Firefox browser and maximize browser window.
     //Create Firefox webdriver instance and launch Firefox browser.
     WebDriver ffDriver = new FirefoxDriver();
     //Maxmize Firefox browser window
  2.  Browse
  3.  Use java print statement to print message “Open website success”.
     //Browse yahoo home page.
     //Sleep 2 seconds to wait for the page load complete
     //Print out action success message.
     System.out.println("Open website success.");
  4.  Input keywords “yahoo” in yahoo search box.
     //Get keyword input text box by it's id.
     WebElement searchBox = ffDriver.findElement("uh-search-box"));
     //Set keyword "yahoo" in search box.
     //Print out action success message.
     System.out.println("Set keyword in search box success.");
  5.  Click search button and wait for the page to show result list.
  6.  Print message “Action success”.
     //Get submit keyword button by it's id.
     WebElement searchButton = ffDriver.findElement("uh-search-button"));
     //Click submit button.
     //Print out action success message
     System.out.println("Action success.");
     //Wait for result list come out.
  7.  Quit and close Firefox browser.
  8. //Quit and close Firefox browser.

How to get web element by id

As you can see from above java code, i use

WebElement searchButton = ffDriver.findElement("uh-search-button"));

to get search button web element object. Webdriver’s findElement method will loop in the entire DOM tree for the loaded webpage, and find the web element which match the By condition, in this case, it use id as the condition. So above line of java code will return web element which id is “uh-search-button”. You can use Chrome inspect or Firefox firebug to get the web element’s id. You can get web element’s id with following steps.

  1. Right click the web element that you want to inspect. In below page, i just right click the search button.
    inspect web elements's attributes with firebug for Firefox
  2. After click “Inspect Element with Firebug” in popup menu you will see another panel appeared which contain html code snippet for the button.
    show html code snippet for the search button
  3. Look at html code highlighted in blue area. The value of id attribute is just the button’s id. You can get the web element in web page dom tree using this id. Then you can call this web element’s click method to click the search button.
     //Get submit keyword button by it's id.
     WebElement searchButton = ffDriver.findElement("uh-search-button"));
     //Click submit button.

    [download id=”272″]

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.