SELENIUM Programs for Interview :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Radio_Checkbox { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Male Radio Button location store into maleRadioBtn WebElement WebElement RadioBtn = driver.findElement(By.id("gender-male")); // Click on Radio Button RadioBtn.click(); // Cricket Checkbox location store into Checkbox WebElement WebElement Cricket = driver.findElement(By.id("cricket")); // Hockey Checkbox location store into Checkbox WebElement WebElement Hockey = driver.findElement(By.id("hockey")); // Select Multiple Checkboxes Cricket.click(); Hockey.click(); } } |
# FindElement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class FindElement { public static void main(String[] args) { // Open browser WebDriver driver = new FirefoxDriver(); // Open Application driver.get("https://www.stqatools.com"); // Click on Home button driver.findElement(By.id("Home")).click(); } } |
# FindElements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class FindElements { public static void main(String[] args) { // Open browser WebDriver driver = new FirefoxDriver(); // Open application driver.get("https://www.stqatoolsl.com"); // Find all elements and store into list List allElement = driver.findElements(By.xpath("//table/tr")); // Get the size of allElements and store into integer. int size = allElement.size(); // Print size of Elements System.out.println(size); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.By; public class Xpath_Selection { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Absolute xpath driver.findElement(By.xpath("html/body/table/tbody/tr[1]/td[1]")); // Relative xpath driver.findElement(By.xpath("//*[@id='menuicon']")); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import static org.testng.Assert.assertEquals; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.testng.Assert; public class Assert_Example { public static void main(String args[]) { String Actual = "Actual"; String Expected = "Expected"; // Assertion Passing Assert.assertTrue(Actual == Actual); System.out.println("Passing 1"); // Assertion failing Assert.fail("Failing 2"); System.out.println("Failing 2"); // Check Assert Equals assertEquals(Actual, Expected); } } |
- driver.get(“URL”);
- driver.getTitle();
- driver.getCurrentUrl();
- driver.getPageSource();
- driver.manage().window().maximize();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Browser_Commands { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub WebDriver driver = new FirefoxDriver(); // Wait For Page To Load driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // Using get command redirect to url driver.get("https://stqatools.com/"); // Get current url Title & store into string String Title = driver.getTitle(); // Get Text of particular element and save into string String Button_Text = driver.findElement(By.id("Button")).getText(); // Get Existing url and save into string String Current_url = driver.getCurrentUrl(); // Get Existing page source and save into string String Page_Source = driver.getPageSource(); // Maximize the Browser driver.manage().window().maximize(); // Minimize the Browser driver.manage().window().setPosition(new Point(0, -1000)); // C the browser or page currently which is having the focus. driver.close(); // Shut down or destroy webdriver instance (Close all the windows) driver.quit(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Screen_shot { public static void main(String[] args) throws IOException { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); // Initialize WebDriver WebDriver driver = new FirefoxDriver(); // Wait For Page To Load driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // Go to URL driver.get("https://www.stqatools.com/"); // Maximize Window driver.manage().window().maximize(); // Take ScreenShot File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); // Store Screenshot FileUtils.copyFile(screenshotFile, new File("D:\\screenshot.png")); // Close Driver driver.quit(); } } |
- driver.close();
- driver.quit();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Navigations { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com/"); // Closes the browser which is currently focused driver.close(); // Closes all browsers opened by driver / Destroys driver object driver.quit(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import java.io.IOException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; public class Desired_Capability { public static void main(String[] args) throws IOException { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); // Initialize WebDriver WebDriver driver = new FirefoxDriver(); // Go to URL driver.get("https://www.stqatools.com/"); // Created object of DesiredCapabilities class DesiredCapabilities capabilities = new DesiredCapabilities(); // Set your device name capabilities.setCapability("deviceName", "your Device Name"); // Set BROWSER_NAME desired capability. capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome"); // Set your mobile device's OS version. capabilities.setCapability(CapabilityType.VERSION, "5.1"); // Set android platformName desired capability. capabilities.setCapability("platformName", "Android"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class Mouse_Hover { public static void main(String[] args) { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Create object of Action class Actions action = new Actions(driver); // Find element using locator and store into WebElement WebElement element = driver.findElement(By.id("elementId")); // Perform Double click operation using action (object) on element. action.doubleClick(element).perform(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.By; public class DropDown_Handle { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Identify Dropdown Without WebElement Select Country = new Select(driver.findElement(By.name("country"))); // Select Country by selectByVisibleText Country.selectByVisibleText("India"); // Select Country by selectByIndex Country.selectByIndex(1); // Select Country by selectByValue Country.selectByValue("91"); // Deselect Country deselectByVisibleText Country.deselectByVisibleText("India"); // Deselect Country deselectByIndex Country.deselectByIndex(1); // Deselect Country deselectByValue Country.deselectByValue("91"); // Deselect All selected Dropdowns Country.deselectAll(); // Check dropdown have MultiSelect option or not? Country.isMultiple(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class Print_WebTable_Data { public static void main(String args[]) { System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); driver.manage().window().maximize(); // Identify the table WebElement table = driver.findElement(By.id("tableId")); // Get all rows (tr tags) List<WebElement> rows = table.findElements(By.tagName("tr")); String Expected = "CellName"; // Print data from each row (Data from each td tag) for (WebElement row : rows) { List<WebElement> cols = row.findElements(By.tagName("td")); for (WebElement col : cols) { System.out.print(col.getText() + "\t"); String Actual = col.getText(); // Check Expected Cell is present or not in WebTable if (Actual.equals(Expected)) { System.out.println("Cell Exist in WebTable..."); } } System.out.println(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class Fluent_Wait { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); WebElement stqatools = driver.findElement(By.id("stqatools")); new WebDriverWait(driver, 10) .pollingEvery(2, TimeUnit.SECONDS) .withTimeout(10, TimeUnit.SECONDS) .until(ExpectedConditions.visibilityOf(stqatools)); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Alert_Popup_Handle { public static void main(String[] args) { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Click on button to show Alert driver.findElement(By.id("Alert_ID")).click(); // Switch to Alert to perform some operation Alert alert = driver.switchTo().alert(); // Accept alert driver.switchTo().alert().accept(); // Dismiss alert driver.switchTo().alert().dismiss(); // GetText of alert driver.switchTo().alert().getText(); // SendKeys to particular alert driver.switchTo().alert().sendKeys("Sandeep"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class DatePicker { WebDriver driver; @BeforeTest public void start(){ System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); driver = new FirefoxDriver(); } @Test public void Test(){ driver.get("http://jqueryui.com/datepicker/"); driver.switchTo().frame(0); driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); //Click on textbox so that datepicker will come driver.findElement(By.id("datepicker")).click(); driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); WebElement dateWidget = driver.findElement(By.id("ui-datepicker-div")); List rows=dateWidget.findElements(By.tagName("tr")); List columns=dateWidget.findElements(By.tagName("td")); for (WebElement cell: columns){ //Select 10th Date if (cell.getText().equals("10")){ cell.findElement(By.linkText("10")).click(); break; } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import java.io.IOException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class DC_Scroll { public static void main(String[] args) throws IOException { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); // Initialize WebDriver WebDriver driver = new FirefoxDriver(); // Go to URL driver.get("https://www.stqatools.com/"); // Return The List of all Cookies driver.manage().getCookies(); // Return specific cookie according to name driver.manage().getCookieNamed(""); // Create and add the cookie driver.manage().addCookie(null); // Delete specific cookie driver.manage().deleteCookie(null); // Delete specific cookie according Name driver.manage().deleteCookieNamed(""); // Delete all cookies driver.manage().deleteAllCookies(); } } |
Ways to Handle Drag and Drop :-
- Drag and Drop using dragAndDrop method.
- DragandDrop using clickAndHold, moveToElement and release method.
1. Drag and Drop using dragAndDrop method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class DragAndDrop { public static void main(String[] args) { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Create object of actions class Actions act=new Actions(driver); // Find element xpath which we need to drag WebElement drag=driver.findElement(By.xpath("put x path")); // Find element xpath where we need to drop WebElement drop=driver.findElement(By.xpath("put x path")); // Drag element to destination act.dragAndDrop(drag, drop).build().perform(); } } |
2. DragandDrop using clickAndHold, moveToElement and release method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class DragAndDrop { public static void main(String[] args) { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Store Drag and Drop location into WebElement WebElement drag = driver.findElement(By.id("Drag_id")); WebElement drop = driver.findElement(By.id("Drop_id")); // Create object of actions class Actions act = new Actions(driver); // Click & Hold drag Webelement act.clickAndHold(drag).build().perform(); // Move to drop Webelement act.moveToElement(drop).build().perform(); // Release drag webelement into drop webelement act.release(drop).build().perform(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import java.io.IOException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class JavaScriptExecutor { public static void main(String[] args) throws IOException { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); // Initialize WebDriver WebDriver driver = new FirefoxDriver(); // Wait For Page To Load driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // Go to URL driver.get("https://www.stqatools.com/"); // Maximize Window driver.manage().window().maximize(); // How to generate Alert Pop window in selenium ? JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("alert('hello world!');"); // How to get innertext of the entire webpage in Selenium? String sText = js.executeScript("return document.documentElement.innerText;").toString(); // How to navigate to different page using Javascript? js.executeScript("window.location = 'https://www.stqatools.com'"); // How to enter value into textbox: // How to click a button: js.executeScript("document.querySelector('#enterimg').click()"); // How to refresh a window: js.executeScript("history.go(0)"); // How to get the text of a particular web element: String text = js.executeScript("return document.getElementById('btn2').innerHTML").toString(); System.out.println("WebElement Text is : " + text); // How to get the title of the WebPage: String title = js.executeScript("return document.title").toString(); System.out.println("Page Title is : " + text); // How to scroll vertically for certain pixels: js.executeScript("window.scrollBy(0,500)"); // How to scroll till the bottom of the web page: js.executeScript("window.scrollTo(0, document.body.scrollHeight)"); // How to scroll to a particular element: js.executeScript("document.querySelector('#countries').scrollIntoView()"); // How to navigate back to page: js.executeScript("window.history.back()"); // How to navigate to forward page: js.executeScript("window.history.forward()"); } } |
1. Handle Mouse Buttons Using Robot class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import java.awt.Robot; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import org.openqa.selenium.By; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import com.gargoylesoftware.htmlunit.javascript.host.geo.Coordinates; public class Robot_Mouse_Class { public static void main(String[] args) { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Create object of Action class Robot robot = new Robot(); // Press Left button robot.mousePress(InputEvent.BUTTON1_MASK); // Release Left button robot.mouseRelease(InputEvent.BUTTON1_MASK); // Press Middle button robot.mousePress(InputEvent.BUTTON2_MASK); // Release Middle button robot.mouseRelease(InputEvent.BUTTON2_MASK); // Press Right button robot.mousePress(InputEvent.BUTTON3_MASK); // Release Right button robot.mouseRelease(InputEvent.BUTTON3_MASK); } } |
2. Program to Scroll Mouse Using Robot class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import java.awt.Robot; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Robot_Mouse_Class { public static void main(String[] args) { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Create object of Action class Robot robot = new Robot(); // Scroll MouseWheel robot.mouseWheel(5); } } |
3. Take ScreenShot Using Robot class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Robot_Mouse_Class { public static void main(String[] args) { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Create object of Action class Robot robot = new Robot(); // Get Screen Size java.awt.Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); // Capture ScreenShot BufferedImage img = robot.createScreenCapture(new Rectangle(size)); // Store image into file File path = new File("D://profile.jpg"); // Write image path ImageIO.write(img, "JPG", path); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Tab_Handle_ForEach { public static void main(String[] args) { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Click on link to open iFrame driver.findElement(By.id("iFrame_Name")).click(); // Switch to Frame driver.switchTo().frame("iframe"); // Perform operation on Frame driver.findElement(By.id("tinymce")).sendKeys("Send text in iframe"); // Switch back to defaultContent driver.switchTo().defaultContent(); // Switch between one frame to another driver.switchTo().frame("iframe_1").switchTo().frame("iframe_2"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class KeyBoard_Sendkeys_Events { public static void main(String[] args) { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Send single key using sendkeys driver.findElement(By.xpath("ele_path")).sendKeys(Keys.ENTER); // Handle multiple keys using chord driver.findElement(By.xpath("//body")).sendKeys(Keys.chord(Keys.CONTROL, "a")); } } |
- id
- xpath
- name
- className
- tagName
- linkText
- partialLinkText
- cssSelector
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Locators { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Locate Element using id driver.findElement(By.id("Element_ID")); // Locate Element using xpath driver.findElement(By.xpath("Element_XPATHEXPRESSION")); // Locate Element using name driver.findElement(By.name("Element_NAME")); // Locate Element using className driver.findElement(By.className("Element_CLASSNAME")); // Locate Element using tagName driver.findElement(By.tagName("Element_TAGNAME")); // Locate Element using linkText driver.findElement(By.linkText("Element_LINKTEXT")); // Locate Element using partialLinkText driver.findElement(By.partialLinkText("Element_Partial_LINKTEXT")); // Locate Element using cssSelector driver.findElement(By.cssSelector("Element_cssSelector")); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Window_Handle { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Current window location store into Parent Window string. String parentWindow = driver.getWindowHandle(); // Click some link that opens a new window. driver.findElement(By.xpath("//*[@id='window_path']")).click(); // Store newly open window into Child Window using getWindowHandles. for (String childWindow : driver.getWindowHandles()) { // Switch to Newly open window using switchTo(). driver.switchTo().window(childWindow); } // Close newly opened window when done with it. driver.close(); // switch back to the Parent window. driver.switchTo().window(parentWindow); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class Mouse_Hover { public static void main(String[] args) { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Find element using locator and store into WebElement WebElement eleToHover = driver.findElement(By.id("eleToHover ")); // Find element using locator and store into WebElement WebElement eletToClick = driver.findElement(By.id("eleToClick ")); // Create object of Action class Actions action = new Actions(driver); // Perform moveToElement operation using action (object) on element. action.moveToElement(eleToHover).click(eleToClick).build().perform(); } } |
- driver.navigate().to(“URL”);
- driver.navigate().back();
- driver.navigate().forward();
- driver.navigate().refresh();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Navigations { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com/"); driver.navigate().to("https://www.stqatools.com/java"); Thread.sleep(5000); driver.navigate().back(); Thread.sleep(5000); driver.navigate().forward(); Thread.sleep(5000); driver.navigate().refresh(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class Tool_Tip { public static void main(String[] args) { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Create action class object Actions builder = new Actions(driver); // find the tooltip xpath WebElement Del_btn_tooltip = driver.findElement(By.xpath("btn_delete")); // Mouse hover to that text message builder.moveToElement(Del_btn_tooltip).perform(); // Extract text from tooltip String tooltip_msg = Del_btn_tooltip.getText(); // Print the tooltip message just for our refrences System.out.println("Tooltip/ Help message is " + tooltip_msg); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; public class ImplicitlyWait { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Wait 30 Seconds until Page loading driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class ChromeDriver{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.stqatools.com"); System.out.println("Title is :-"+driver.getTitle()); driver.close(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class GeckoDriver { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); System.out.println("Title is :- "+driver.getTitle()); driver.close(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class InternetExplorerDriver{ public static void main(String[] args) { WebDriver driver=new HtmlUnitDriver(); driver.get("https://www.stqatools.com"); System.out.println("Title is :-"+driver.getTitle()); driver.close(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; public class InternetExplorerDriver{ public static void main(String[] args) { System.setProperty("webdriver.ie.driver","D:\\IEDriverServer.exe"); WebDriver driver=new InternetExplorerDriver(); driver.get("https://www.stqatools.com"); System.out.println("Title is :-"+driver.getTitle()); driver.close(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.safari.SafariDriver; public class SafariDriver{ public static void main(String[] args) { WebDriver driver = new SafariDriver(); driver.get("https://www.stqatools.com"); System.out.println("Title is :-"+driver.getTitle()); driver.close(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class pageLoadTimeout_Wait { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class Mouse_Hover { public static void main(String[] args) { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Create object of Action class Actions action = new Actions(driver); // Find element using locator and store into WebElement WebElement element = driver.findElement(By.id("elementId")); // Perform Right click operation using action (object) on element. action.contextClick(element).perform(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.io.IOException; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class Actions_Scroll { public static void main(String[] args) throws IOException { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); // Initialize WebDriver WebDriver driver = new FirefoxDriver(); // Go to URL driver.get("https://www.stqatools.com/"); // Create object of Actions class Actions actions = new Actions(driver); // Scroll Down using Actions class actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform(); // Scroll Up using Actions class actions.keyDown(Keys.CONTROL).sendKeys(Keys.HOME).perform(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.awt.Robot; import java.awt.event.KeyEvent; import java.io.IOException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Robot_Scroll { public static void main(String[] args) throws IOException { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); // Initialize WebDriver WebDriver driver = new FirefoxDriver(); // Go to URL driver.get("https://www.stqatools.com/"); // Create a Robot class object Robot robot = new Robot(); // Scroll Down using Robot class robot.keyPress(KeyEvent.VK_PAGE_DOWN); robot.keyRelease(KeyEvent.VK_PAGE_DOWN); // Scroll Up using Robot class robot.keyPress(KeyEvent.VK_PAGE_UP); robot.keyRelease(KeyEvent.VK_PAGE_UP); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class JavaScriptExecutor_Scroll { public static void main(String[] args) throws IOException { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); // Initialize WebDriver WebDriver driver = new FirefoxDriver(); // Wait For Page To Load driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // Go to URL driver.get("https://www.stqatools.com/"); // Maximize Window driver.manage().window().maximize(); // Create JavascriptExecutor object jes JavascriptExecutor jse = (JavascriptExecutor) driver; // Scroll Down page using First way jse.executeScript("window.scrollBy(0,250)", ""); // Scroll Up Page using First way jse.executeScript("window.scrollBy(0,-250)", ""); // OR // Scroll Down Page using Second way jse.executeScript("scroll(0, 250);"); // Scroll Up Page using Second way jse.executeScript("scroll(0, -250);"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class setScriptTimeout_Wait { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); driver.manage().timeouts().setScriptTimeout(500, TimeUnit.SECONDS); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class All_Checkbox_Select { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // All checkbox locations store into list using findelements List<WebElement> els = driver.findElements(By.xpath("//input[@type='checkbox']")); // Using for each loop store all checkboxes into el for (WebElement el : els) { // Verify if checkbox in not selected then click on that checkbox if (!el.isSelected()) { el.click(); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; public class Thread_Sleep_Wait { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Wait 5 Seconds Compulsory Thread.sleep(5000); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Verify_Title { public static void main(String[] args) { // Set gecko driver location System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); // Create object of webdriver WebDriver driver = new FirefoxDriver(); // Open url driver.get("https://stqatools.com"); // Wait until page load driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // Get page title String Actual = driver.getTitle(); // Get Expected page title String Expected = "Software Testing Quality Automation Tutorials"; // compare the actual title with expected one if (Actual.equals(Expected)) { System.out.println("Test Passed!"); } else { System.out.println("Test Failed"); } driver.close(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.WebDriverWait; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; public class WebDriver_Wait { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); WebElement element = driver.findElement(By.id("id")); // Create a Object wait of WebdriverWait class WebDriverWait wait = new WebDriverWait(driver,30); // Using ExpectedConditions wait until element visibility wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("i"))); // Wait until alert present on page wait.until(ExpectedConditions.alertIsPresent()); // Wait until element to be clickable on page wait.until(ExpectedConditions.elementToBeClickable(element)); // Wait until element to be selectable on page wait.until(ExpectedConditions.elementToBeSelected(element)); // Wait until frame is available and frame selected. wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(element)); // Wait and check element is invisibility wait.until(ExpectedConditions.invisibilityOf(element)); // Wait until present element located by. wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy((By) element)); // Wait until text present on particular an element wait.until(ExpectedConditions.textToBePresentInElement(element, "Text")); // Wait until element value present for a particular element. wait.until(ExpectedConditions.textToBePresentInElementValue(element, "Value")); // check element visibility wait.until(ExpectedConditions.visibilityOf(element)); // Wait and check title contains or not. wait.until(ExpectedConditions.titleContains("Tilte")); } } |
- Clear Command
- Click Command
- Submit Command
- IsDisplayed Command
- IsEnabled Command
- IsSelected Command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class WebElement_Commands { public static void main(String[] args) throws InterruptedException { WebDriver driver = new FirefoxDriver(); // Wait For Page To Load. driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // Using get command redirect to url. driver.get("https://stqatools.com/"); // Clear particular WebElement e.g: Textbox. driver.findElement(By.id("TextBox")).clear(); // Send value to particular WebElement e.g: Textbox. driver.findElement(By.id("TextBox")).sendKeys("Sandeep"); // Click on any WebElement e.g: Button. driver.findElement(By.id("Button_Id")).clear(); // Verify WebElement is Present or Not? driver.findElement(By.id("Text")).isDisplayed(); // Verify WebElement is Enabled or Not? e.g: Radio / Checkbox. driver.findElement(By.id("Text")).isEnabled(); // Verify WebElement is Selected or Not? e.g: Radio / Checkbox driver.findElement(By.id("Text")).isSelected(); // Submit the form & perform action. driver.findElement(By.id("Submit")).submit(); // Get text of Particular WebElement & Store into String String Text = driver.findElement(By.id("TextBox")).getText(); // Get TagName of Particular WebElement & Store into String String TagName = driver.findElement(By.id("TextBox")).getTagName(); // Get Attribute of Particular WebElement & Store into String String Attr = driver.findElement(By.id("id")).getAttribute("attr"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import java.util.ArrayList; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Tab_Handle_SwitchTo { public static void main(String[] args) { // Set Gecko Driver location. System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Store all currently open tabs in tabs2 ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles()); // Click on link to open in new tab driver.findElement(By.id("Url_Link")).click(); // Switch newly open Tab driver.switchTo().window(tabs2.get(1)); // Perform some operation on Newly open tab // Close newly open tab after performing some operations. driver.close(); // Switch to old(Parent) tab. driver.switchTo().window(tabs2.get(0)); } } |
- Java and Selenium are the best automation tools for QA. And these skills are essential for each QA engineer involved in test automation.
- Therefore, Above programs, we are presenting a set of Selenium programs to help test automation developers during job interviews.
Selenium Programs for Interview :- Navigation in Selenium,Close and Quit Driver in Selenium,Verify title in Selenium,Launch Firefox Browser,Launch Chrome Browser,Launch Safari Browser,Launch IE Explorer,Launch HtmlUnitDriver Headless Browser,Browser Commands,WebElement Commands,FindElement and FindElements,Locators,Checkbox and Radio Button,DropDown and Multi Select Box,Select All Checkboxes,Dynamic WebTable,Handle Calendar / JQuery Date Picker,Assert and Verify,Absolute and Relative Xpath,ImplicitWait,Thread,Sleep,WebDriver Wait,FluentWait,PageLoadTimeout Command,setScriptTimeout Command,Handle Newly Open Window,Handle Alert and Popup,Window Tab,Handle iFrame,Handle Drag and Drop,Mouse Hover,Right Click,Double Click,Handle ToolTip,Handle KeyBoard Events,Handle Events Using RobotClass,Capture Screenshot,Handle Events Using JavaScriptExecutor,Scroll Page using JavaScriptExecutor class,Scroll Down and Scroll Up page using Robot Class,Scroll Down and Scroll Up page Using Actions Class,Desired Capability,Handle Cookie in Selenium. Frequently Asked Selenium Interview Questions and Answers and 50 Most Popularly Asked Selenium Interview Questions and Answers and The Best Selenium Interview Questions & Answers Top Selenium Interview Question and Answers for 2019
Selenium Programs for Interview Selenium Programs for Interview Selenium Programs for Interview