Skip to main content

Selenium Tutorial I

Create your first script in webdriver

Today we are going to learn how to create the selenium script in webdriver.  

First, create a new java class according to the below steps. 

1. Create a new java project. 










































2. Enter the following details in the "New Java Project" pop up window.












































* Project name - Google
* Location to save the project - Click on checkbox to use default location
* JRE - Select " Use an execution environment JRE "
* Project Layout - Click on "Create separate folders for sources and class files"

then click on Finish button.

3. Right click on Google project and add new package for the selected project.












































4. Enter the valid name for the name of the package in the "New Java Package" pop up window.
































* Name - GoogleSearch

then click on Finish button.

5. Right click on GoogleSearch package and add new class for the selected package.









































6. Enter the valid name for the name of the package in the "New Java Class" pop up window.








































* Name - GooglevalidateSearch

then click on Finish button.

7. For adding the external JARs, The following steps should be followed.
       
    *  Right click on "Google" project and select "Properties".
    *  Select "Java Build Path".
    *  Click on "Add External JARs".
    *  Select the jar files.
    *  Click on "Open" button.
    *  Click on "Apply" button.
    *  Click on "OK" button.




























































8.  Open GooglevalidateSearch java class

The following figure is shown the actual webdriver code. 












For starting to use webdriver API, The following two package should be imported.

org.openqa.selenium.WebDriver
org.openqa.selenium.firefox.FirefoxDriver












The above packages has not been used yet. According to the tasks, the relevant packages should be imported.

*   org.openqa.selenium.WebDriver - This contains webdriver class. This needs to instantiate a new          browser loaded with specific driver.

*   org.openqa.selenium.firefox.FirefoxDriver - This contains Firefox class.  This needs to instantiate      a firefox specific driver onto the browser instantiated by the webdriver class.


The webdriver object is instantiated as follows.

       static WebDriver driver = new FirefoxDriver()

The default Firefox will be launched  because there are no parameters for FirefoxDriver class.

For getting the base URL, the getURL()  method is written as follows.  

      public static void getURL()
    {
    System.out.println("Calling getURL()");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    String baseurl="https://www.google.lk/?gfe_rd=cr&ei=uxdqWJHGLpCEogO_64LQCg";
    driver.get(baseurl); 
    System.out.println("Launch Firefox and direct it to the Base URL");
    }

The getURL() method is calling inside the main method.

     public static void main(String[] args) throws Exception 
          {
// Launch Firefox and direct it to the Base URL
getURL();
 }































    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)  -  An implicit wait is to tell             webDriver to poll the DOM for a certain amount time when trying to find an element if they               are not immediately available. The default setting is zero. 

    driver.manage().window().maximize() - This is used for maximizing the window.    

    String baseurl="https://www.google.lk/?gfe_rd=cr&ei=uxdqWJHGLpCEogO_64LQCg" -  The                base URL is assigned to the variable. 

    driver.get(baseurl) - The get() method is used to launch a new browser session. The get() method             directs it to the URL. The baseurl is specified as its parameter. 































     driver.close()  - This method is used to close the browser window.

    System.exit(0) - This is used to terminate the program.


9. Run the test

Click on Run button to run GooglevalidateSearch java class
   








See you soon with another selenium tutorial...

Thanks, 
Dananji Withana




Comments

  1. Well Done dear..!!! Keep it up.
    Its better if you can present your source codes using gist or something like that instead of screenshots. It will be useful for readers. (https://gist.github.com/)

    ReplyDelete
  2. Very supportive and await for the next selenium post!
    Keep it up!!!

    ReplyDelete

Post a Comment

Popular posts from this blog

Java basic for Selenium - Selenium Tutorial II

Today we are talking about Array. The software tester should have some basic knowledge about java when writing selenium scripts for the validations. Array is a data structure where stored in elements in same data type. The declaration and initialization an array as follows. int[]  Array_a;   or  int  Array_a[]; - This is an integer array. int[10] = Array_num;  - This is an integer array which stores 10 elements. Array_num[0] = 10;    - Assign value 10 to 0 th element of array. (Index [0]) int[]   Array_num = { 1, 2, 3, 4, 5 };  Finding maximum value of an array Finding minimum value of an array Finding average value of an array Find the key of an array Reverse an array The time complexity of this algorithm is   O(n/2)   which is O(N) because we are iterating over array till midpoint only. The initial iteration is happening as follows The...

Security Testing - I - Web Testing - I

Security Testing - Web Testing  Security is set of measures to protect an application against unpredictable actions that causes it to terminate functioning. Security testing is a process intended to reveal flaws in the security mechanisms of an information system that protect data and maintain functionality as intended. This is type of non functional testing. For minimizing the defects by identifying threats in the system and cost of quality, the security testing must be started at the early stage of Software development life cycle. The following figure is shown the relation between Software development life cycle and security testing . Test plan should includes Test scenarios and test cases related to the security. Test data related to the security test cases. Test tools for security testing and test outputs on the different test tools Security Testing Approach 1. Identify all the business requirements, security goals ...

Strategies and methods for test case design II

Strategies and methods for test case design Today we are talking about how to get an approach for writing a good test case. Developers cannot prevent or eliminate all the defects raised in the application during implementation. That's why the application should be tested before deliver to the customer. Before starting the execution, we need to identify test scenarios and write test cases for each identified scenarios. Test case is test description which needs to be executed to verify the functionality or feature of the application. A good test case have a good possibility of finding defects. For designing effective test cases, there are two basic strategies that can be used. Black box test strategies White box test strategies Test Strategies Sources Testing Techniques Black Box  Functional requirement specification Equivalence partitioning   Business requirement specification Boundary value analysis   Domain knowledge State Transition Testing     ...