C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Selenium WebDriver- First Test CaseIn this section, you will learn how to create your First Selenium Automation Test Script. Under this test, we will automate the following scenarios:
We will create our test case step by step to give you a complete understanding of each component in detail. Step1. Launch Eclipse IDE and open project "Demo_Test" which we have created in the previous section (Configure Selenium WebDriver) of this Tutorial. We will write our first Selenium test script in the "First.class" file under the "Demo_Test" test suite. Note: To invoke a browser in Selenium, we have to download an executable file specific to that browser. For example, Chrome browser implements the WebDriver protocol using an executable called ChromeDriver.exe. These executable files start a server on your system which in turn is responsible for running your test scripts in Selenium.Step2. Open URL: https://sites.google.com/a/chromium.org/chromedriver/downloads in your browser. Step3. Click on the "ChromeDriver 2.41" link. It will redirect you to the directory of ChromeDriver executables files. Download as per the operating system you are currently on. For windows, click on the "chromedriver_win32.zip" download. The downloaded file would be in zipped format. Unpack the contents in a convenient directory. Note: Selenium developers have defined properties for each browser that needs the location of the respective executable files to be parsed in order to invoke a browser. For example, the property defined for Chrome browser - webdriver.chrome.driver, needs the path of its executable file - D:\ChromeDriver\chromedriver.exe in order to launch chrome browser.Step4. We would need a unique identification for the web elements like Google Search text box and Search button in order to automate them through our test script. These unique identifications are configured along with some Commands/Syntax to form Locators. Locators help us to locate and identify a particular web element in context of a web application. The method for finding a unique identification element involves inspection of HTML codes.
Step5. Now it is time to code. We have embedded comments for each block of code to explain the steps clearly. import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class First { public static void main(String[] args) { // declaration and instantiation of objects/variables System.setProperty("webdriver.chrome.driver", "D:\\ChromeDriver\\chromedriver.exe"); WebDriver driver=new ChromeDriver(); // Launch website driver.navigate().to("http://www.google.com/"); // Click on the search text box and send value driver.findElement(By.id("lst-ib")).sendKeys("TheDeveloperBlog tutorials"); // Click on the search button driver.findElement(By.name("btnK")).click(); } } The Eclipse code window will look like this: Step6. Right click on the Eclipse code and select Run As > Java Application. Step7. The output of above test script would be displayed in Google Chrome browser. Explanation of the CodeImport Packages/StatementsIn java, import statements are used to import the classes present in another packages. In simple words, import keyword is used to import built-in and user-defined packages into your java source file.
Instantiating objects and variablesA driver object is instantiated through: WebDriver driver=new ChromeDriver(); Launch WebsiteTo launch a new website, we use navigate().to() method in WebDriver. driver.navigate().to("http://www.google.com/"); Click on an elementIn WebDriver, user interactions are performed through the use of Locators which we would discuss in later sessions of this tutorial. For now, following instance of code is used to locate and parse values in a specific web element. driver.findElement(By.id("lst-ib")).sendKeys("TheDeveloperBlog tutorials");
Next TopicWebDriver Commands
|