Selenium Basics Tips
Selenium Basics Tips
1| Maximize Firefox browser window using selenium webdriver
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
#PYTHON
driver.maximize_window()
#RUBY
require "selenium-webdriver"
@driver.manage.window.maximize
#PYTHON
driver.maximize_window()
#RUBY
require "selenium-webdriver"
@driver.manage.window.maximize
2| Customize browser window size
driver.manage().window().setSize(new Dimension(320, 480));
driver.manage().window().setSize(new Dimension(320, 480));
#PYTHON
driver.set_window_size(1920, 500)
#RUBY
@driver.manage.window.resize_to(300,700)
System.out.println(driver.getCurrentUrl());
#PYTHON
print driver.current_url
#RUBY
puts @driver.current_url
4| Navigate Back | Forward & Page refresh
Navigate Back
Webdriver driver = new FirefoxDriver();
driver.navigate().back();
or|
Actions actions = new Actions(driver);
actions.sendKeys(Keys.BACK_SPACE).perform();
#PYTHON
driver.back()
#RUBY
@driver.navigate.back
Navigate Forward
driver.navigate().forward();
#PYTHON
driver.forward()
#RUBY
@driver.navigate.forward
Navigate URL
driver.navigate().to("https://www.google.co.in/");
#RUBY
@driver.navigate.to "https://www.google.co.in/"
Page Refresh
driver.navigate().refresh();
or|
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
or|
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("history.go(0)");
or|
driver.navigate().to(driver.getCurrentUrl());
or|
driver.findElement(By.id("firstname-placeholder")).sendKeys("uE035");
or|
driver.findElement(By.id("filter-box")).sendKeys(Keys.F5);
#PYTHON
driver.refresh()
#RUBY
@driver.navigate.refresh
5| Highlighting Elements
Excercise|1|
@Test
public void highlighttest() throws Exception {
driver.get("www.whatever.com");
WebElement searchbutton = driver.findElement(By.id("Value"));
highlightElement(searchbutton);
WebElement submitbutton = driver.findElement(By.id("Value"));
highlightElement(submitbutton);
element.click();
}
public void highlightElement(WebElement element) {
for (int i = 0; i < 2; i++) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute(style, arguments[1]);", element, "color: yellow; border: 2px solid yellow;");
js.executeScript("arguments[0].setAttribute(style, arguments[1]);", element, "");
}
Navigate Back
Webdriver driver = new FirefoxDriver();
driver.navigate().back();
or|
Actions actions = new Actions(driver);
actions.sendKeys(Keys.BACK_SPACE).perform();
#PYTHON
driver.back()
#RUBY
@driver.navigate.back
Navigate Forward
driver.navigate().forward();
#PYTHON
driver.forward()
#RUBY
@driver.navigate.forward
Navigate URL
driver.navigate().to("https://www.google.co.in/");
#RUBY
@driver.navigate.to "https://www.google.co.in/"
driver.navigate().refresh();
or|
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
or|
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("history.go(0)");
or|
driver.navigate().to(driver.getCurrentUrl());
or|
driver.findElement(By.id("firstname-placeholder")).sendKeys("uE035");
or|
driver.findElement(By.id("filter-box")).sendKeys(Keys.F5);
#PYTHON
driver.refresh()
#RUBY
@driver.navigate.refresh
5| Highlighting Elements
WebElement element1 = driver.findElement(By.className("Value"));
WebElement element2 = driver.findElement(By.id("Value"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].setAttribute(style, arguments[1]);", element1, "color: blue; border: 2px solid blue;");
jse.executeScript("arguments[0].setAttribute(style, arguments[1]);", element2, "color: yellow; border: 0px solid red;");
WebElement element2 = driver.findElement(By.id("Value"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].setAttribute(style, arguments[1]);", element1, "color: blue; border: 2px solid blue;");
jse.executeScript("arguments[0].setAttribute(style, arguments[1]);", element2, "color: yellow; border: 0px solid red;");
Excercise|1|
@Test
public void highlighttest() throws Exception {
driver.get("www.whatever.com");
WebElement searchbutton = driver.findElement(By.id("Value"));
highlightElement(searchbutton);
WebElement submitbutton = driver.findElement(By.id("Value"));
highlightElement(submitbutton);
element.click();
}
public void highlightElement(WebElement element) {
for (int i = 0; i < 2; i++) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute(style, arguments[1]);", element, "color: yellow; border: 2px solid yellow;");
js.executeScript("arguments[0].setAttribute(style, arguments[1]);", element, "");
}
6| Multi-Select Elements
Excercise|1|
@Testpublic void Multiselect() throws Exception {
driver.get("http://www.ryancramer.com/journal/entries/select_multiple/");
List<WebElement> ele = driver.findElements(By.tagName("select"));
System.out.println(ele.size());
WebElement ele2 = ele.get(0);
List<WebElement> ele3 = ele2.findElements(By.tagName("option"));
System.out.println(ele3.size());
ele2.sendKeys(Keys.CONTROL);
ele3.get(0).click();
ele3.get(1).click();
ele3.get(3).click();
ele3.get(4).click();
ele3.get(5).click();
}
7| Double-click WebElement
Actions action = new Actions(driver);
action.doubleClick(driver.findElement(By.id("Value")));
action.perform();
8| Delete All Cookies
driver.manage().deleteAllCookies();
9| Shortcut to open IDE in the same screen
Ctrl + shift + S

10| Open a New Tab
or|
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
11| Open a New Window
public void Test01() throws Exception {
openTab("http://www.xyz.com");
}
public void trigger(String script, WebElement element) {
((JavascriptExecutor) driver).executeScript(script, element);
}
public Object trigger(String script) {
return ((JavascriptExecutor) driver).executeScript(script);
}
public void openTab(String url) {
String script = "var d=document,a=d.createElement(a);a.target=_blank;a.href=%s;a.innerHTML=.;d.body.appendChild(a);return a";
Object element = trigger(String.format(script, url));
if (element instanceof WebElement) {
WebElement anchor = (WebElement) element;
anchor.click();
trigger("var a=arguments[0];a.parentNode.removeChild(a);", anchor);
} else {
throw new JavaScriptException(element, "Unable to open Window", 1);
}
}
12| Select Dropdown Box
Select dropdown = new Select(driver.findElement(By.name("jump")));
dropdown.selectByIndex(1);
Select has more options to play with; Some of the main functions are given below,
dropdown.deselectAll(); // Used while handling multi-select
dropdown.selectByVisibleText("");
dropdown.selectByValue("");
dropdown.deselectByIndex(3);
dropdown.deselectByValue("3");
dropdown.deselectByVisibleText("");
13| Starting Selenium Server
Open cmd. Go to server location and paste the below
14| All Old versions of IDE available on:
http://release.seleniumhq.org/selenium-ide/
15| Entire Selenium Functions:
http://selenium.googlecode.com/svn/tags/selenium-2.0.alpha-5/docs/api/java/index-all.html
16| Check Firefox versions and its suitable Drivers:
http://selenium.googlecode.com/git/py/CHANGES
17| Selenium Release Notes [Change_Log]
Check my answer on Stackoverflow:
http://stackoverflow.com/questions/18609557/where-to-find-selenium-webdriver-release-notes/22243556#22243556
18| Right-click WebElement
Actions builder = new Actions(driver);
Action rightClick = builder.contextClick(driver.findElement(By.id("Value"))).build();
rightClick.perform();
Note:- There will be frequent updates in this section, Selenium Basics & Tips.
#Please comment your thoughts and the topics you need in this blog.