Tuesday, October 4, 2016

My first Selenium code - Automation

Let's create First Automated test case using Selenium Web driver.
Right click on the Java project src folder and create a new class and name it "FirstTestCase".





Copy and paste below code in the newly created class.

public class FirstTestCase {

public static void main(String[] args) {
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
        //Launch the website
driver.get("http://www.amazon.com");

        // Print a Log In message to the screen
        System.out.println("Successfully opened the website");

//Wait for 5 Sec
Thread.sleep(5);
        // Close the driver
        driver.quit();
    }
}


Hover over the red error messages and import browser drivers.

It will add import statement at the top saying ‘import org.openqa.selenium.WebDriver;’

The import statement in Java allows referring to classes that are declared in other packages to be accessed without referring to the full package name.


This will add the statement ‘import org.openqa.selenium. safari.SafariDriver;‘ and the error will be gone.



Once you resolve these error one more error will pop up at the statement ‘Thread.sleep(5);‘,

This time it will ask to select the ‘Add throws declaration‘ and after selecting that it will add ‘throws InterruptedException‘ at the method declaration.

Now, Run the Java test. To start the test just select Run > Run As > Java Application Or Right Click on Eclipse code and Click Run As  > Java Application.


After a few Seconds Safari Browser will open and you will see that with the help of your script, Selenium will Launch the Online Store, display the Successful Message and Close the browser.



Once the execution is finished, you will see the message in Console section of the Eclipse IDE displaying:


No comments:

Post a Comment