How to take screenshot from xvfb display centos 6 centos 7

To debug the tests in no display servers with XVFB display. Using ImageMagick

– Install imagemagick – sudo yum install ImageMagick
– Suppose the browser runs at display :99 using xvfb

Then

– In linux terminal type – export DISPLAY=:99
– Then type – “import -window root example.png”

The above command will create a screenshot of given display screen -in png format.

How to setup grid to run firefox in selenium 3

To invoke firefox in selenium 3, you have to parse -Dwebdriver.gecko.driver=AndPathToGECKOexecutable parameter with the hub

java -jar -Dwebdriver.gecko.driver=geckodriver.exe selenium-server-standalone-3.0.0-beta3.jar -role hub

Make sure the parameter should be before selenium jar

Selenium 3 geckodriver automation tests

Selenium 3 stopped support for FirefoxDriver , It will work only up to firefox version 46.

To work with latest firefox browsers, you have to use Gecko Driver

  • Download latest geckodriver from https://github.com/mozilla/geckodriver/releases

Then set system property “webdriver.gecko.driver” using –
System.setProperty(“webdriver.gecko.driver”,
“pathToGeckoDriver\\geckodriver.exe”);

 


if (SystemUtils.IS_OS_WINDOWS) {
System.setProperty("webdriver.gecko.driver",
"drivers\\geckodriver.exe");
} else if (SystemUtils.IS_OS_LINUX) {
System.setProperty("webdriver.gecko.driver", "drivers/geckodriver");

}

Then simply call 

WebDriver driver = new FirefoxDriver();
driver.get();

 

browser resizing using selenium


import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestWindowResize {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://google.co.in");
System.out.println(driver.manage().window().getSize());
Dimension dimension = new Dimension(1024, 600);
driver.manage().window().setSize(dimension);
System.out.println(driver.manage().window().getSize());
}
}

delete cookies using selenium webdriver


import java.util.Set;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class DeleteCookies {

public static WebDriver driver = null;

public static void main(String[] args) {
driver = new FirefoxDriver();
driver.get("http://google.com");

Set<Cookie> co = driver.manage().getCookies();
System.out.println(co.size());
// delete all the cookies of a webpage
driver.manage().deleteAllCookies();

// should be 0 since all the cookies have been deleted
Set<Cookie> coA = driver.manage().getCookies();
System.out.println(coA.size());

driver.get("http://google.com");
Set<Cookie> coB = driver.manage().getCookies();

// get the cookie name
for (Cookie cookie : coB) {
System.out.println(cookie);
}

// delete a single cookie using name
driver.manage().deleteCookieNamed("NID");

Set<Cookie> coC = driver.manage().getCookies();
for (Cookie cookie : coC) {
System.out.println("NIC is removed: " + cookie);
// Delete cookie
driver.manage().deleteCookie(cookie);
}
}
}

Kill a process using webdriver org.openqa.selenium.os.WindowsUtils


import org.openqa.selenium.os.WindowsUtils;

public class KillProcess {
public static void main(String[] args) {
WindowsUtils.tryToKillByName("notepad.exe");
}
}

Tools for web element locators