Selenium with java

9

Click here to load reader

Transcript of Selenium with java

Page 1: Selenium with java

Selenium with Java

Page 2: Selenium with java

What is WebDriver?

• WebDriver is a web automation framework, allows you to execute your tests against different browsers, not just Firefox.

• It Supports almost all browser• It uses programming languages:• JAVA• PHP• Python• PERL• Many more..

Page 3: Selenium with java

Guide to install Selenium WebDriver

Step 1 - Install Java on your computer (Latest JDK) Go to http://ninite.com and choose the JDK.Step 2 - Install Eclipse IDEGo to https://ninite.com/Step 3 - Download the Selenium Java Client Driverhttp://docs.seleniumhq.org/download/

Page 4: Selenium with java

• Launch the "eclipse.exe" file inside the "eclipse" folder.

• When asked to select for a workspace, just accept the default location.

• Create a new project through File > New > Java Project. Name the project as "newproject".

• A new pop-up window will open enter details as follow1. Project Name2. Location to save project3. Select an execution JRE4. Select layout project option5. Click on finish button

Configure Eclipse IDE with WebDriver

Page 5: Selenium with java

• In this step,• Right-click on the newly created project and• Select New > Package, and name that package as

"newpackage".• A pop-up window will open to name the package,Enter the name of the packageClick on finish button• Create a new Java class under newpackage by right-

clicking on it and then selecting- New > Class, and then name it as "MyClass". Your Eclipse IDE should look like the image below.

Configure Eclipse IDE with WebDriver

Page 6: Selenium with java

• When you click on Class, a pop-up window will open, enter details as

• Name of the class• Click on Finish button• Now selenium WebDriver's into Java Build Path• In this step,• Right-click on "newproject" and select Properties.• On the Properties dialog, click on "Java Build Path".• Click on the Libraries tab, and then • Click on "Add External JARs.."

Configure Eclipse IDE with WebDriver

Page 7: Selenium with java

• package mypackage;•  • import org.openqa.selenium.WebDriver;• import org.openqa.selenium.firefox.FirefoxDriver;•  • public class myclass {•  •     public static void main(String[] args) {•         // declaration and instantiation of objects/variables•         WebDriver driver = new FirefoxDriver();•         String baseUrl = "http://newtours.demoaut.com";•         String expectedTitle = "Welcome: Mercury Tours";•         String actualTitle = "";•  •         // launch Firefox and direct it to the Base URL•         driver.get(baseUrl)

First Webdriver code

Page 8: Selenium with java

•  // get the actual value of the title•         actualTitle = driver.getTitle();•  •         /*•          * compare the actual title of the page witht the expected one and print•          * the result as "Passed" or "Failed"•          */•         if (actualTitle.contentEquals(expectedTitle)){•             System.out.println("Test Passed!");•         } else {•             System.out.println("Test Failed");•         }•         •         //close Firefox•         driver.close();•         •         // exit the program explicitly•         System.exit(0);•     }•  • }

First Webriver code

Page 9: Selenium with java

• WebDriver provides these useful get commands: • get() • getTitle() • getPageSource() • getCurrentUrl() • getText()

WebDriver get commands