automationScript

Common webpage operations using Selenium

Selenium provides so many capabilities to perform test automation which makes it easy to perform any webpage based task. This page has all the reusable code blocks which are required for commonly used webpage operations during test automation with Selenium webdriver and Java such as: highlight webelement in selenium, mouseover in selenium or mouse hover in selenium using Actions, extract css, take a screenshot, upload file, scroll up ,down, switch frames,,  etc. Some methods may use javascript with selenium. 

How to use mouseover in Selenium

Actions Actions = New Actions(Driver);
WebElement MenuElement = Driver.FindElement(By.Id("Menu-Element-Id"));
Actions.MoveToElement(MenuElement).MoveToElement(Driver.FindElement(By.Xpath("Xpath-Of-Menu-Item-Element"))).Click();

Extract CSS Attribute Of An Element

String TextColor = Driver.FindElement(By.Id("Id123")).GetCssValue("Color");

Execute A JavaScript Statement On Page

JavascriptExecutor Jsx = (JavascriptExecutor) Driver;
Jsx.ExecuteScript("Alert('Hi')");

Upload A File On A Page

String FilePath = "Path\To\Filefor\Upload";
JavascriptExecutor Jsx = (JavascriptExecutor) Driver;
Jsx.ExecuteScript("Document.GetElementById('FileName').Value='" + FilePath + "';");

Scroll Up, Down Or Anywhere On A Page

JavascriptExecutor Jsx = (JavascriptExecutor) Driver;
//Vertical Scroll - Down By 100 Pixels
Jsx.ExecuteScript("Window.ScrollBy(0,100)", "");
//Vertical Scroll - Up By 55 Pixels (Note The Number Is Minus 55)
Jsx.ExecuteScript("Window.ScrollBy(0,-55)", "");
//Horizontal Scroll - Right By 20 Pixels
Jsx.ExecuteScript("Window.ScrollBy(20,0)", "");
//Horizontal Scroll - Left By 95 Pixels (Note The Number Is Minus 95)
Jsx.ExecuteScript("Window.ScrollBy(-95,0)", "");

Get HTML Source Of A Element On Page

JavascriptExecutor Jsx = (JavascriptExecutor) Driver;
String ElementId = "Element-Id";
String Html =(String) Jsx.ExecuteScript("Return Document.GetElementById('" + ElementId + "').InnerHTML;");

Switch Between Frames In Java Using Webdriver

WebElement FrameElement = Driver.FindElement(By.Id("Id-Of-Frame"));
Driver.SwitchTo().Frame(FrameElement);

How to Type in Selenium without using sendKeys() method

FirefoxDriver driver=new FirefoxDriver();
// Maximize the window
driver.manage().window().maximize();
// Open applicatiion
driver.get("enter your application URL");
// This will execute JavaScript in your script
((JavascriptExecutor)driver).executeScript("document.getElementById('some id').value='mukesh'");

Click in Selenium if button or radio button is disable

FirefoxDriver driver=new FirefoxDriver();
// Maximize the window
driver.manage().window().maximize();
// Open applicatiion
driver.get("enter your application URL");
// This will execute JavaScript in your script
((JavascriptExecutor)driver).executeScript("document.getElementById('enter your element id').click()");

Subscribe for updates