Top 25 Selenium Webdriver Commands
It covers almost all selenium webdriver commands with syntax. This tutorial acts as your guide to all important Selenium Webdriver commands.
For detailed description refer Browser Commands with Program Example
For detailed tutorial refer - Web Form Commands with Program Example
1. Browser Commands
Browser commands are the basic commands in Selenium. These commands are the starting point of your selenium script. Browser commands are used to launch browser, maximize it, open url and close browser etc.
Syntax of all browser commands:
/************************** * Browser Commands * ************************** */ // Set Path of driver executable String driver_executable_path = "./src/com/techlistic/utils/geckodriver.exe"; System.setProperty("webdriver.gecko.driver", driver_executable_path); // Launch Browser - Creating a Firefox instance WebDriver driver = new FirefoxDriver(); // For Chrome WebDriver driver = new ChromeDriver(); // For Safari WebDriver driver = new SafariDriver(); // For Opera WebDriver driver = new OperaDriver(); // For IE WebDriver driver = new InternetExplorerDriver(); // Open URL driver.get("https://www.techlistic.com/"); // Or String url = "https://www.techlistic.com/"; driver.get(url); // Get Title driver.getTitle(); // Code implementation String pageTitle = driver.getTitle(); // Validate Page Title if(pageTitle == "Techlistic - Home") { System.out.println("Test Passed."); } else { System.out.println("Test Failed."); } // Get Current URL driver.getCurrentUrl(); //Code implementation String pageURL = driver.getCurrentUrl(); // Validate Current Page URL if(pageURL == "https://www.techlistic.com/p/selenium-tutorials.html") { System.out.println("Test Passed."); } else { System.out.println("Test Failed."); } // Get Page Source driver.getPageSource(); // Or String sourceCode = driver.getPageSource(); // Close Current Browser Window driver.close(); // Close All Browser Windows driver.quit(); // Maximize Browser driver.manage().window().maximize(); // Make Browser Window Full Screen driver.manage().window().fullscreen(); // Minimize the browser driver.manage().window().setPosition(new Point(0, -1000)); // Set Size of browser driver.manage().window().setSize(new Dimension(1920, 1080));
For detailed description refer Browser Commands with Program Example
2. Browser Navigation Commands
These commands are used to move forward and back using browser history.
Syntax for browser navigation commands:
/***************************
* Browser Navigation Commands * *********************************** */ // Navigate Forward driver.navigate().forward(); // Navigate Back driver.navigate().back(); // Refresh Page driver.navigate().refresh(); // Naviagte directly to some URL (which might be external or internal in the website) driver.navigate().to("https://www.techlistic.com/p/java.html");
3. Find Element Commands
These commands are used to find the gui elements on the web page.
Syntax for all find element commands:
/**************************
* Find Element Commands *
**************************
*/
// Find Single WebElemnt
driver.findElement(arg0);
// Find Multiple WebElements, it returns a list of WebElements
driver.findElements(arg0);
// Find by different locators
driver.findElement(By.className("class-name"));
driver.findElement(By.cssSelector("some-css-selctor"));
driver.findElement(By.id("some-id"));
driver.findElement(By.linkText("some-link-text"));
driver.findElement(By.name("some-name"));
driver.findElement(By.partialLinkText("some-partial-link-text"));
driver.findElement(By.tagName("some-html-tag-name"));
driver.findElement(By.xpath("some-xpath-expression"));
// Find web element and store value in WebElement variable for re-usability
WebElement element = driver.findElement(By.xpath("/some/xpath");
4. Wait Commands
Wait commands are used to tell Selenium that for how much time it has to wait for an element on a web page before throwing ElementNotFound exception. Or in simple words we can say it is the expected time to load the page or element on the page.
Syntax for Wait Commands:
/******************************** * Wait Commands * ******************************** * a. Implicit Wait * b. Explicit Wait * c. Fluent Wait */ // Implicit wait - Set wait of 10 seconds driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Explicit Wait WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.alertIsPresent()); // Fluent Wait Wait wait = new FluentWait(WebDriver reference) .withTimeout(timeout, SECONDS) .pollingEvery(timeout, SECONDS) .ignoring(Exception.class); WebElement foo = wait.until(new Function() { public WebElement applyy(WebDriver driver) { return driver.findElement(By.id("foo")); } });
5. Web Form Commands
These commands are used to perform basic gui actions like., enter text, click and select dropdowns.
Syntax for all web form commands:
/******************************** * Form Commands * ******************************** */ /* sendKeys() * This command is used for entering value in a Text box */ driver.findElement(By.xpath("/some/xpath")).sendKeys("Some Value"); // OR // Implementation with Webelement WebElement FIRSTNAME = driver.findElement(By.xpath("/some/xpath")); FIRSTNAME.sendKeys("Any Text value"); // Clear Command - to clear the already filled data in text box driver.findElement(By.xpath("/some/xpath")).clear(); /* getText() Command * This command is used to get value/text from a webelement */ String textValue = driver.findElement(By.xpath("/some/xpath")).getText(); /* Click Command * click() */ driver.findElement(By.xpath("/some/xpath")).click(); /* Select Class * selectByIndex() * selectByVisibleText() * selectByValue() */ WebElement DROP_DOWN = driver.findElement(By.xpath("/select")); // Create object of Select Class Select select1 = new Select(DROP_DOWN); // Option 1 select1.selectByIndex(2); // Option 2 select1.selectByVisibleText("Edam");
For detailed tutorial refer - Web Form Commands with Program Example
Handle Window Dialog (Upload File) << Previous || Next >> Automation Framework Building 1st Step (Code Resuseability)
Follow Us
Feel free to ask queries or share your thoughts in comments or email us.
Comments
Post a Comment