C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Selenium WebDriver- CommandsAs we have discussed earlier in the IDE section, Selenium commands are the set of commands that are used to run our Selenium tests. In Selenium WebDriver, we have an entirely different set of commands for performing different operations. Since we are using Selenium WebDriver with Java, commands are simply methods written in Java language. Note: A java method is a collection of statements that are grouped together to perform a specific operation.Before getting into the details of commands provided by Selenium WebDriver, we insist you to go through the Java OOPs (Object-Oriented Programming) concepts in java programming language. You can also refer to our Java OOPs concepts section provided in the Java Tutorial. Now, the question arises is that how can we access methods provided by WebDriver. Till now, we have successfully created our first test script in Selenium WebDriver. Therefore, one possible way to view the methods provided by WebDriver is to open the Eclipse IDE loaded with Selenium Webdriver jar files, create a driver object for WebDriver and press the dot key. It will show you all of the possible methods provided by WebDriver. Let us consider an example form the suggestions displayed by Eclipse to understand the syntax of the methods provided by WebDriver. Method NameTo access any method of any class, we need to create an object of class and then all the public methods will appear for the object. ParameterA parameter is an argument which is passed to a method to perform some specific operation. Return typeMethods can return a value or returning nothing (void). If the void is mentioned after the method, it means, the method is returning no value. If it is returning a value, then it must display the type of the value for e.g. getTitle(): String. Now, we will discuss the various commands provided by WebDriver. The commands provided by Selenium WebDriver can be broadly classified in following categories: Given below are some of the most commonly used Selenium commands in WebDriver: 1. Fetching a web pageThere are two methods to fetch a web page:
driver.get("www.TheDeveloperBlog.com")
driver.navigate().to("https://TheDeveloperBlog.com/selenium-tutorial"); 2. Locating forms and sending user inputsdriver.findElement(By.id("lst-ib")).sendKeys("TheDeveloperBlog tutorials"); 3. Clearing User inputsThe clear() method is used to clear the user inputs from the text box. driver.findElement(By.name("q")).clear(); 4. Fetching data over any web elementSometimes we need to fetch the text written over a web element for performing some assertions and debugging. We use getText() method to fetch data written over any web element. driver.findElement(By.id("element567")).getText(); 5. Performing Click eventThe click() method is used to perform click operation on any web element. driver.findElement(By.id("btnK")).click(); 6. Navigating backward in browser historydriver.navigate().back(); 7. Navigating forward in browser historydriver.navigate().forward(); 8. Refresh/ Reload a web pagedriver.navigate().refresh(); 9. Closing Browserdriver.close(); 10. Closing Browser and other all other windows associated with the driverdriver.quit(); 11. Moving between Windowsdriver.switchTo().window("windowName"); 13. Moving between Framesdriver.switchTo().frame("frameName"); 14. Drag and DropDrag and Drop operation is performed using the Action class. WebElement element = driver.findElement(By.name("source")); WebElement target = driver.findElement(By.name("target")); (new Actions(driver)).dragAndDrop(element, target).perform(); Let us consider a sample test script which will cover most of the commonly used WebDriver commands. For our test purpose, we are using a dummy web page under the URL: https://www.testandquiz.com/selenium/testing.html The default interface of the web page looks like this: You can also use this dummy web page for your Selenium Testing practice. First you need to download the browser driver for the browser on which you are willing to automate your test scenarios. We have already discussed execution of Selenium test scripts on different browsers in the previous sections of this tutorial. For this test, we are using Firefox Gecko driver to automate our test scenarios on Firefox browser. Below is the sample test script with embedded comments. import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.support.ui.Select; public class Second { public static void main(String[] args) { // System Property for Gecko Driver System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" ); // Initialize Gecko Driver using Desired Capabilities Class DesiredCapabilities capabilities = DesiredCapabilities.firefox(); capabilities.setCapability("marionette",true); WebDriver driver= new FirefoxDriver(capabilities); // Launch Website driver.navigate().to("https://www.testandquiz.com/selenium/testing.html"); // Fetch the text "This is sample text." and print it on console // Use the class name of the div to locate it and then fetch text using getText() method String sampleText = driver.findElement(By.className("col-md-12")).getText(); System.out.println(sampleText); // Use the linkText locator method to find the link and perform click using click() method driver.findElement(By.linkText("This is a link")).click(); // Click on the textbox and send value driver.findElement(By.id("fname")).sendKeys("JavaTpoint"); // Clear the text written in the textbox driver.findElement(By.id("fname")).clear(); // Click on the Submit button using click() command driver.findElement(By.id("idOfButton")).click(); // Locate the radio button by id and check it using click() function driver.findElement(By.id("male")).click(); // Locate the checkbox by cssSelector and check it using click() function driver.findElement(By.cssSelector("input.Automation")).click(); // Use Select class for selecting value from dropdown Select dropdown = new Select(driver.findElement(By.id("testingDropdown"))); dropdown.selectByVisibleText("Automation Testing"); // Close the Browser driver.close(); } }
Next TopicWebDriver - Browser Commands
|