Search

Revising Basic Selenium Commands

Updated: Jan 5, 2021



1. Creating a webdriver object:


//for firefox driver
WebDriver driver = new FirefoxDriver();
//for chrome driver
WebDriver driver = new ChromeDriver();

2. Driver get Commands:


//to launch a URL
driver.get("URL");
//to get page titile
driver.getTitle();
//to get current URL of the page
driver.getCurrentUrl();
//to get page source
driver.getPageSource()
//to get the parent window name as a String
driver.getWindowHandle();
//to get names of all open windows
Set<String>s=driver.getWindowHandles();


3. Driver Navigate commands:


//to navigate forward
driver.navigate().forward();
//to navigate back
driver.navigate().back();
//to launch URL
driver.navigate.to("URL");
//to refresh page
driver.navigate().refresh();


4. Driver find element commands:


//to find a single element
WebElement element = driver.findElement(By.id("id"));
//to find list of elements
List<WebElement> elements = driver.findElements(By.tagName("tagname"));

//other locations in place of By.id 
By.className("classname")
By.name("name")
By.cssSelector("css")
By.linkText("text")
By.partialLinkText("text")
By.xpath("//*xpath")


5. Element Operations:


//to click an element
driver.findElement(By.locator("locating element")).click();
//to enter text in textbox
driver.findElement(By.locator("locating element")).senKeys("text");
//to get text of the element
String text = driver.findElement(By.locator("locating element")).getText();
//returns TagName of the element 
String value = driver.findElement(By.locator("locating element")).getTagName();
//to clear the text field
driver.findElement(By.locator("locating element")).clear();
//to get attribute value
driver.findElement(By.locator("locating element")).getAttribute("src");
//returns true if element is enabled else returns false
boolean value = driver.findElement(By.locator("locating element")).isEnabled();
//returns true if element is checked else returns false
boolean value = driver.findElement(By.locator("locating element")).isSelected();
// Returns height, width, x and y coordinates referenced element
Rectangle res =  driver.findElement(By.cssSelector("h1")).getRect();
// Rectangle class provides getX,getY, getWidth, getHeight methods
System.out.println(res.getX());


6. SwitchTo commands:


//to switch to a frame
driver.switchTo().frame("WebElement");
//to switch to a mentioned window
driver.switchTo().window("Window handle element")
// Opens a new tab and switches to new tab
driver.switchTo().newWindow(WindowType.TAB);
// Opens a new window and switches to new window
driver.switchTo().newWindow(WindowType.WINDOW);
// Return to the top level
driver.switchTo().defaultContent();
//to switch to alert
driver.switchTo().alert();

7. Driver Manage commands:


//to get fullscreen
driver.manage().window().fullscreen();
//to maximize the window
driver.manage().window().maximize();

//Access each dimension individually
int width = driver.manage().window().getSize().getWidth();
int height = driver.manage().window().getSize().getHeight();

//Or store the dimensions and query them later
Dimension size = driver.manage().window().getSize();
int width1 = size.getWidth();
int height1 = size.getHeight();

//to manage timeouts
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


8. Closing the browser:


//to close the browser window or tab
driver.close();
//to quit at end of the session
driver.quit();

9. Alert:


//Wait for the alert to be displayed
wait.until(ExpectedConditions.alertIsPresent());
//Store the alert in a variable
Alert alert = driver.switchTo().alert();
//Store the alert text
String text = alert.getText();
//Press the OK button
alert.accept();
//Press the Cancel button
alert.dismiss();
//Type your message
alert.sendKeys("text");

10. Select option:


//creating select object
Select selectObject =new Select(selectElement);
// Select an <option> based upon the <select> element's internal index
selectObject.selectByIndex(1);
// Select an <option> based upon its value attribute
selectObject.selectByValue("value1");
// Select an <option> based upon its text
selectObject.selectByVisibleText("text");
// Return a List<WebElement> of options that have been 
selectedList<WebElement> allSelectedOptions = selectObject.getAllSelectedOptions();
// Return a List<WebElement> of options that the <select> element containsList<WebElement> allAvailableOptions = selectObject.getOptions();
// Deselect an <option> based upon the <select> element's internal index
selectObject.deselectByIndex(1);
// Deselect an <option> based upon its value attribute
selectObject.deselectByValue("value1");
// Deselect an <option> based upon its text
selectObject.deselectByVisibleText("text");
// Deselect all selected <option> elements
selectObject.deselectAll();
//does this allow multiple selection
 Boolean doesThisAllowMultipleSelections = selectObject.isMultiple();
 

11. Action Class:


//to create action object
Actions actionProvider =new Actions(driver);

Mouse Actions:
// Perform click-and-hold action on the element      actionProvider.clickAndHold(Element).build().perform();
// Perform context-click action on the element      actionProvider.contextClick(Element).build().perform();
// Perform double-click action on the element      actionProvider.doubleClick(Element).build().perform();
// Performs mouse move action onto the element      actionProvider.moveToElement(Element).build().perform();
// Performs drag and drop action of sourceEle onto the targetEle      actionProvider.dragAndDrop(sourceEle, targetEle).build().perform();
// Performs release event     
 actionProvider.release().build().perform();

Keyboard Actions:
//to press a key down
Action keydown = actionProvider.keyDown(Keys.CONTROL).sendKeys("a").build();   keydown.perform();
//to release the key from pressing down
action.keyDown(Keys.SHIFT).sendKeys(Element,"text").keyUp(Keys.SHIFT).sendKeys("text").perform();


Happy Testing!!!



27 views0 comments

Recent Posts

See All

A string s is called good if there are no two different characters in s that have the same frequency. Given a string s, return the minimum number of characters you need to delete to make s good. The f

The numeric value of a lowercase character is defined as its position (1-indexed) in the alphabet, so the numeric value of a is 1, the numeric value of b is 2, the numeric value of c is 3, and so on.