In this blog, you will understand about one of the approach to write automation commands in Selenium WebDriver.
A common approach used by most automation engineers is to use the generic selenium commands like
In the code snippet shown above generic selenium commands are used to first fill the textbox with a text value and then clicked the button. You can expand the snippet shown above to perform a Google search as shown below in a complete Java example:
Below are the line by line code explanation for above Java program:
Above code is package structure for current program file.
Following code is for required packages.
Following code is to initialize the Firefox driver.
below code is to maximize the driver instance.
Following code is to open the link in browser.
You can also use the following code is to open the link in browser.
Following code is to find the file input type webelement on the page.
Following code is to type into webelement.
Following code is to find the file input type webelement on the page.
Following code is to click on the webelement.
Following code is to quit the webdriver instance.
You can also use the following code is to close the webdriver instance.
Output of the above program should be:
A common approach used by most automation engineers is to use the generic selenium commands like
click()
sendKeys()to perform their automation task. This is shown in the example below:
WebElement textbox = driver.findElement(By.name("q")); textbox.sendKeys("selenium commands"); WebElement button = driver.findElement(By.name("btnG")); button.click();
In the code snippet shown above generic selenium commands are used to first fill the textbox with a text value and then clicked the button. You can expand the snippet shown above to perform a Google search as shown below in a complete Java example:
Java Source code:
package com.helloselenium.selenium.test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class UsingGenericSeleniumCommands { public static void main(String args[]){ WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("http://google.com"); WebElement textbox = driver.findElement(By.name("q")); textbox.sendKeys("selenium commands"); WebElement button = driver.findElement(By.name("btnG")); button.click(); driver.quit(); } }
Below are the line by line code explanation for above Java program:
package com.helloselenium.selenium.test;
Following code is for required packages.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver;
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
Following code is to open the link in browser.
driver.get("http://google.com");
driver.navigate().to("http://google.com");
WebElement textbox = driver.findElement(By.name("q"));
textbox.sendKeys("selenium commands");
WebElement button = driver.findElement(By.name("btnG"));
button.click();
driver.quit();
driver.close();
0 Comments
What would you like to add in my list? I look forward to reading your comments below.