Selenium WebDriver First Automation Script for Beginners

This post will show you how to write your first Selenium Webdriver script. It will cover some of the very basic Webdriver commands. It covers following web actions and commands for same:
  1. Launch browser.
  2. Open demo url.
  3. Enter value in some text fields.
  4. Select Radio box.
  5. Select Checkbox.
  6. Click on Submit button
public class FirstWebdriverScript {

public static void main(String[] args) {

// Initialize Webdriver Object
System.setProperty("webdriver.chrome.driver", "D:\\mydir\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String browserUrl = "https://www.techlistic.com/p/selenium-practice-form.html";
String firstName = "Mark";

// Opening Browser URL and Maximize Window
driver.get(browserUrl);
driver.manage().window().maximize();

// Page Elements or locators
WebElement FIRST_NAME_FIELD = driver.findElement(By.name("firstname"));
WebElement RADIO_BUTTON = driver.findElement(By.id("sex-1"));
WebElement PROFESSION = driver.findElement(By.name("profession"));
WebElement SUBMIT_BUTTON = driver.findElement(By.name("submit"));

// Clear pre-values in textfield
FIRST_NAME_FIELD.clear();

// Enter Text
FIRST_NAME_FIELD.sendKeys(firstName);

// Select Radio Button
RADIO_BUTTON.click();

// Select Profession
PROFESSION.click();

// Click on Submit Button
SUBMIT_BUTTON.click();

// Close browser
driver.close()

}

}



Form Commands (Click, Enter Text etc.)  << Previous     ||     Next >>  Handle Links


Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.

Comments

Popular posts from this blog

10 Best Demo Websites for Automation Testing Practice

Automate 'User Registration' of E-commerce Website using Selenium

Automate Browser Actions on Godaddy.com using Selenium

Automate E-Commerce Website using Selenium Webdriver