Configuring PhantomjsDriver to run selenium test cases - java

Am new to this PhantomjsDriver in selenium webdriver.I need to run my selenium scripts in server without GUI. Please can anybody tell me how to achieve this. I need a head's up from the start of how to configure Phantomjs Driver,usage in server and rest.Below is my selenium code which i run through GUI , now i have to run these cases in server without GUI. What modifications i have to do so hat i can achieve the above task.
public static void main(String[] args) throws IOException{
login =args[0];
user = args[1];
pwd = args[2];
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setOutputDirectory(args[3]);
testng.setTestClasses(new Class[] {
CreateMultiRecordTest.class, UpdateMultiRecordTest.class,
DeleteMultiRecordTest.class
});
testng.addListener(tla);
testng.run();

Finally after a weeks time i found a solution to configure PhantomJs for my framework.Here's the solution.
DesiredCapabilities cap = new DesiredCapabilities();
java.io.File f = new java.io.File("");
String path = f.getAbsolutePath()+"\\ghostdriver-master\\src\\main.js";
cap.setCapability(PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY,path);
driver = new PhantomJSDriver(cap);

This worked for me:
DesiredCapabilities dCaps = new DesiredCapabilities();
dCaps.setJavascriptEnabled(true);
dCaps.setCapability("takesScreenshot", false);
dCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"C:\\phantomjs-1.9.7-windows\\phantomjs.exe");
PhantomJSDriver driver = new PhantomJSDriver(dCaps);
...

Related

Appium error while instantiating the object

This is a BeforeClass that I was creating (Java, Appium, TestNG)
private AndroidDriver driver;
#BeforeClass
public void setUp() throws MalformedURLException{
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability(MobileCapabilityType.APPIUM_VERSION, "1.7.1");
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");
desiredCapabilities.setCapability("deviceName", "9NLJA17619012618");
desiredCapabilities.setCapability("appActivity", ".activity.LaunchActivity");
desiredCapabilities.setCapability("appPackage", "com.aaa.app");
desiredCapabilities.setCapability("platformVersion", "7.1.2");
URL url = new URL("http://localhost:4723/wd/hub");
driver = new AndroidDriver(url,desiredCapabilities);
}
(I know there is a mix of MobileCapabilityType and others without that format, but that i not the point I think)
And then a bunch of #Test
This error appeared:
Failed tests: setUp(tests.TestShop):
class io.appium.java_client.android.AndroidDriver has interface io.appium.java_client.AppiumDriver as super class
Any Clue?
add MobileElement or WebElement while initializing appium driver.
driver = new AndroidDriver<MobileElement>(url,desiredCapabilities);
Also change your deviceName to udid. Also make sure your appActivity is correct.
Your code must be like following:
#BeforeClass
public void setUp() throws MalformedURLException{
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("platformName","Android");
desiredCapabilities.setCapability("deviceName", "Any name");
desiredCapabilities.setCapability("udid", "9NLJA17619012618");
desiredCapabilities.setCapability("appActivity", ".activity.LaunchActivity");
desiredCapabilities.setCapability("appPackage", "com.aaa.app");
desiredCapabilities.setCapability("platformVersion", "7.1.2");
URL url = new URL("http://localhost:4723/wd/hub");
driver = new AndroidDriver<MobileElement>(url,desiredCapabilities);
}

Unecessary logs in headless testing using phantom js and selenium

I am getting so many unnecessary logs in red color while performing headless testing using phantom js.
How to remove all those red color logs
public class Utility
{
private static WebDriver driver=new PhantomJSDriver();
public static WebDriver getDriver()
{
return driver;
}
}
Here is what you need to do to suppress the INFO logs:
File src = new File("C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path", src.getAbsolutePath());
DesiredCapabilities dcap = new DesiredCapabilities();
String[] phantomArgs = new String[] {
"--webdriver-loglevel=NONE"
};
dcap.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomArgs);
PhantomJSDriver driver = new PhantomJSDriver(dcap);
driver.get("https://www.facebook.com/");
Let me know if it works for you.

Selenium don't recognize driver with jenkins

When I execute the jenkins job, the selenium test always fail with this error
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property
or when I set the driver path
not found /var/jenkins/.../C:/selenium/drivers/chromedrive
I have the follow environment
1 jenkins server and selenium hub running on linux
1 selenium node running on Windows.
The selenium node is running with the follow line
java -Dwebdriver.chrome.driver=C:/selenium/drivers/chromedriver.exe -jar selenium-server-standalone-2.53.1.jar -port 5556 -role node -hub http://192.168.15.99:4444/grid/register -browser "browserName=chrome, version=ANY, maxInstances=10, platform=WINDOWS"
Selenium hub and node can see each other.
Why I can't execute the tests? Look's like selenium are trying to execute on the hub, not on the node. How can I configure to do not ask for Chrome driver location?
My test
public class TesteSelenium{
private static final String APLICATION_CONTEXT = "/SYSA";
WebDriver driver;
HomePage home;
#Before
public void setUp() {
Properties p = PropertiesUtil.getProperties();
File file = new File(p.getProperty("webdriver.path"));
System.setProperty(p.getProperty("webdriver.type"), file.getAbsolutePath());
driver = new ChromeDriver();
driver.get(p.getProperty("host.address")+APLICATION_CONTEXT);
LoginPage login = PageFactory.initElements(driver, LoginPage.class);
login.setUsuarioTextField(p.getProperty("usuario.selenium.login"));
login.setSenhaPasswordField(p.getProperty("usuario.selenium.password"));
home = login.submit();
}
#After
public void finish() {
driver.close();
}
I use a properties file
host.address = http://jbossserver:8080
usuario.selenium.login = USER_SELENIUM
usuario.selenium.password = 123123
webdriver.path = C:/selenium/drivers/chromedriver
webdriver.type = webdriver.chrome.driver
You should have your parameters inside quotation marks in command line. Like this:
java -Dwebdriver.chrome.driver="C:/selenium/drivers/chromedriver.exe"
The code to run Selenium tests remotely is slightly different.
public void setUp() throws MalformedURLException {
Properties p = PropertiesUtil.getProperties();
//File file = new File(p.getProperty("webdriver.path"));
//System.setProperty(p.getProperty("webdriver.type"), file.getAbsolutePath());
DesiredCapabilities capability = DesiredCapabilities.chrome();
//driver = new ChromeDriver();
WebDriver driver = new RemoteWebDriver(new java.net.URL("http://seleniumHubIp:4444/wd/hub"), capability);
driver.get(p.getProperty("host.address")+APLICATION_CONTEXT);
LoginPage login = PageFactory.initElements(driver, LoginPage.class);
login.setUsuarioTextField(p.getProperty("usuario.selenium.login"));
login.setSenhaPasswordField(p.getProperty("usuario.selenium.password"));
home = login.submit();
}
My mistake was writing the code to run local tests on a remote selenium node.

Selenium Automation a new window handle

So after my code runs a new window shows up to download a CSV. A window pops up to either save the file or to open it with excel. How would I change windows to download that csv to a path using selenium.
public class automation {
public static void main(String[] args) {
FirefoxProfile profile = new FirefoxProfile();
String path = "C:\\Users\\K344975\\Desktop";
profile.setPreference("browser.download.dir",path);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/xls;text/csv");
profile.setPreference("plugin.disable_full_page_plugin_for_types",
"application/octet-stream;application/csv;text/csv;application/vnd.ms-excel;application/xls;");
WebDriver driver = new FirefoxDriver(profile);
driver.get("");
String winHandle = driver.getWindowHandle();
System.out.println(winHandle);
//driver.quit();
}
}
I also tried making a profile
Well you have firstly patch profile and only after it create browser, and for plugin disable you could use
profile.setPreference("plugin.disable_full_page_plugin_for_types",
"application/octet-stream;application/csv;text/csv;application/vnd.ms-excel;")

Browsermob is not working with selenium webdriver

public class Test_One
{
public static void main(String[] args) throws Exception {
ProxyServer server = new ProxyServer(8105);
server.start();
server.setCaptureHeaders(true);
server.setCaptureContent(true);
server.newHar("test");
DesiredCapabilities capabilities = new DesiredCapabilities();
Proxy proxy = server.seleniumProxy();
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(true);
profile.setPreference("network.proxy.http", "localhost");
profile.setPreference("network.proxy.http_port", 8105);
profile.setPreference("network.proxy.ssl", "localhost");
profile.setPreference("network.proxy.ssl_port", 8105);
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.no_proxies_on", "");
//profile.setProxyPreferences(proxy);
profile.setPreference(key, value)
capabilities.setCapability(FirefoxDriver.PROFILE,profile);
capabilities.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("http://www.google.com");
Har har1 = server.getHar();
}
}
I'm new to selenium and broswermob. This is my code. Whe I try to execute this and getting error The method setProxyPreferences(Proxy) is undefined for the type FirefoxProfile. How to solve this?
You (generally) don't need to configure the FirefoxProfile settings manually. The Using With Selenium section of the readme file gives an example of how to use the proxy with Selenium:
// start the proxy
ProxyServer server = new ProxyServer(4444);
server.start();
// get the Selenium proxy object
Proxy proxy = server.seleniumProxy();
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);
// start the browser up
WebDriver driver = new FirefoxDriver(capabilities);
// create a new HAR with the label "yahoo.com"
server.newHar("yahoo.com");
// open yahoo.com
driver.get("http://yahoo.com");
// get the HAR data
Har har = server.getHar();
If that doesn't work, it may be a problem with your specific version of Firefox and/or Selenium and/or BrowserMob Proxy. What versions are you using? A stack trace with the exact error message would also help.
Try removing profile from capabilities and passing it directly to FirefoxDriver:
driver = new FirefoxDriver(new FirefoxBinary(),profile,capabilities);

Categories