Selenium WebDriver - First Automation Script which covers all basic commands

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()

}

}

For complete list of Selenium tutorials click here: Selenium Webdriver Tutorials Series

Join Telegram channel at https://t.me/techlistic
Feel free to ask queries or share your thoughts in comments or email me.

Comments

Popular Posts

Top 10 Demo Websites to Practice Selenium Webdriver Online

Top 25 Must to know Selenium Webdriver Commands List

Assignment 5: Automate "Buy Product" Functionality for E-commerce Website using Selenium

Selenium Essentials - How to become expert in Automation Testing?

Selenium WebDriver - Browser and Navigation Commands