Selenium Tips and Tricks.

by thulashi 2014-01-02 10:52:39

Verifying Color
String headerColor = driver.findElement(By.xpath(“.//*[@id='content_subheader']“)).getCssValue(“background-color”);
// System.out.println(“Actual value: ” + headerColor);
Assert.assertEquals(“some message”, “rgba(141, 209, 208, 1)”, headerColor);

Taking screenshots
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Copy screenshot and save
FileUtils.copyFile(scrFile, new File(Path you want the screenshot to be saved.png”));

Finding element by link
driver.findElement(By.linkText(“Home”)).click();
Finding element by xpath driver.findElement(By.xpath(“//div[@id='ocr']/div/div/ul/li[5]/a”)).click();

Selecting from dropdown list
driver.findElement(By.id(“Field116″)).click();
new Select(driver.findElement(By.id(“Field116″))).selectByVisibleText(“Dr”)

Maximizing screen
driver.manage().window().maximize();

Running selenium in Internet Explorer
System.setProperty(“webdriver.ie.driver”, “Path to InternetExplorerDriver.exe”);
driver = new InternetExplorerDriver();

Running selenium in Chrome
System.setProperty(“webdriver.chrome.driver”, “Path to chromedriver.exe”);
driver = new ChromeDriver();

Scroll to bottom of page
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript(“window.scrollBy(0,450)”, “”);

Interacting with IFrames
driver.switchTo().frame(“wufooForms7x1x7″);
//switch back
driver.switchTo().defaultContent();

Browser Back and Forward (Navigation)
driver.navigate().back();
driver.navigate().forward();
528
like
0
dislike
0
mail
flag

You must LOGIN to add comments