How to connect phantomjs to selenium using java - java

I have a problem with my code - see below. Could somebody tell me what is wrong? It won't connect but everything is correct.
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.testng.annotations.Test;
public class Main {
#Test
public static void main(String[] args){
File src = new File("phan//bin//phantomjs");
// System.out.println("test:" + File);
System.setProperty("phantomjs.binary.path",src.getAbsolutePath());
WebDriver driver = new PhantomJSDriver();
driver.get("http://facebook.com");
System.out.println(driver.getTitle());
}
}

Try following:
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
...
File phantomJSBinary = new File("path" + File.separator + "to" + File.separator + "phantomjs");
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomJSBinary.getAbsolutePath());
WebDriver driver = new PhantomJSDriver(caps);
...

Related

How disable debuger on chrome webdriver in Java?

My target application use <script type="text/javascript">debugger;</script> So my page is blocked by break point.
How disable debugger on chrome webdriver in Java?
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import com.github.noraui.utils.Utilities.OperatingSystem;
import com.github.noraui.utils.Utilities.SystemArchitecture;
public class Bot {
public static void main(String[] args) {
final OperatingSystem currentOperatingSystem = OperatingSystem.getCurrentOperatingSystem();
String pathWebdriver = String.format("src/test/resources/drivers/%s/googlechrome/%s/chromedriver%s", currentOperatingSystem.getOperatingSystemDir(),
SystemArchitecture.getCurrentSystemArchitecture().getSystemArchitectureName(), currentOperatingSystem.getSuffixBinary());
System.setProperty("webdriver.chrome.driver", pathWebdriver);
ChromeOptions options = new ChromeOptions();
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://rd.huangpuqu.sh.cn/website/html/shprd/shprd_tpxw/List/list_0.htm");
}
}
I think, add javascipt code (revert of debugger;) by this:
((JavascriptExecutor) driver).executeScript("...");
EDIT WITH HEADLESS MODE + SCREENSHOT:
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import com.github.noraui.utils.Utilities.OperatingSystem;
import com.github.noraui.utils.Utilities.SystemArchitecture;
public class Bot {
public static void main(String[] args) throws IOException {
final OperatingSystem currentOperatingSystem = OperatingSystem.getCurrentOperatingSystem();
String pathWebdriver = String.format("src/test/resources/drivers/%s/googlechrome/%s/chromedriver%s", currentOperatingSystem.getOperatingSystemDir(),
SystemArchitecture.getCurrentSystemArchitecture().getSystemArchitectureName(), currentOperatingSystem.getSuffixBinary());
System.setProperty("webdriver.chrome.driver", pathWebdriver);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);
driver.get("http://rd.huangpuqu.sh.cn/website/html/shprd/shprd_tpxw/List/list_0.htm");
final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
FileUtils.forceMkdir(new File(System.getProperty("user.dir") + File.separator + "downloadFiles"));
FileUtils.writeByteArrayToFile(new File(System.getProperty("user.dir") + File.separator + "downloadFiles" + File.separator + "bot.jpg"), screenshot);
}
}
The result is same, my sceenshot is white.

Not found constructor AndroidDriver with Appium in eclipse

Code with errors:
package TestCase;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.gargoylesoftware.htmlunit.javascript.host.URL;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;
public class TestWebBrowser {
//AppiumDriver driver = new IOSDriver();
public static AndroidDriver driver;
public static void main(String[] args) throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
}
The message error is:
The constructor URL(string) is undefined
The constructor AndroidDriver(URL, DesiredCapabilities) is undefined
AndroidDriver is a raw type
I have tried with different versions of java-client and still the problem persists
You need to use an existen constructor like this:
https://appium.github.io/java-client/io/appium/java_client/android/AndroidDriver.html
You need use java.net.URL and not com.gargoylesoftware.htmlunit.javascript.host.URL
#Lorena, hi.
1. Firstly, could You please double check the imports ? Sharing code snippet below with correct ones
package tests.web;
import java.net.MalformedURLException;
import java.net.URL;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileBrowserType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
public class AndroidWebTest {
private static final String ACCESS_KEY = System.getenv(“SEETEST_IO_ACCESS_KEY”);
private static final String CLOUD_URL = “https://cloud.seetest.io:443/wd/hub”;
private static final String TITLE = “Testing Website on Android Chrome with Java”;
private AndroidDriver driver = null;
#Before
public void setUp() throws MalformedURLException {
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(“testName”, TITLE);
dc.setCapability(“accessKey”, ACCESS_KEY);
dc.setBrowserName(MobileBrowserType.CHROME);
driver = new AndroidDriver(new URL(CLOUD_URL), dc);
}
#Test
public void testAppiumOnChrome() {
driver.get(“https://amazon.com”);
System.out.println(driver.getTitle());
if (driver.getCapabilities().getCapability(“device.category”).equals(“TABLET”)) {
driver.findElement(By.xpath(“//*[#name=’field-keywords’]”)).sendKeys(“iPhone”);
driver.findElement(By.xpath(“//*[#text=’Go’]”)).click();
} else {
driver.findElement(By.xpath(“//*[#name=’k’]”)).sendKeys(“iPhone”);
driver.findElement(By.xpath(“//*[#value=’Go’]”)).click();
}
}
#After
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
Please see the Comparing and combining web and mobile test automation drivers article for more details.
If You project is maven-based, could You please also double check the dependencies?
For example, please see latest appium updates here
Appropriate maven repo to check for (latest) java client:
https://mvnrepository.com/artifact/io.appium/java-client

"driver cannot be resolved" in my Java program [duplicate]

This question already has answers here:
"driver cannot be resolved" in Java program
(2 answers)
Closed 3 years ago.
I am very new to programming. My main goal is to write code that logs into a android app and does everything that you would have to do with your fingers. So far I've only got the app to open, now I'm stuck on entering the login information.
I am not familiar with every aspect that has to do with Java, Eclipse, Appium, and Selenium.
Here is my code:
package OpenOfferUpTest;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
public class OpenOfferUp {
#Test
public void OpensOfferUp() throws MalformedURLException
{
File OfferUp = new File("C:\\Users\\boung\\Desktop\\OfferUp.apk");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "Virtual Device");
cap.setCapability("platformName", "android");
cap.setCapability("null", "OfferUp");
cap.setCapability("appPackage", "com.offerup");
cap.setCapability("appActivity", "com.offerup.android.login.splash.LoginSplashActivity");
AndroidDriver driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), cap);
}
#Test
public void SimpleTest() {
driver.findElement(By.id("email_button")).sendKeys("sourgta#gmail.com");
}
}
At driver, it is underlined red, and says that it cannot be resolved. I am not sure on how to fix this.
These are the options I am given:
package OpenOfferUpTest;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import org.testng.annotations.*;
import io.appium.java_client.android.AndroidDriver;
public class OpenOfferUp {
AndroidDriver driver;
#BeforeTest
public void OpensOfferUp() throws MalformedURLException
{
File OfferUp = new File("C:\\Users\\boung\\Desktop\\OfferUp.apk");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "Virtual Device");
cap.setCapability("platformName", "android");
cap.setCapability("null", "OfferUp");
cap.setCapability("appPackage", "com.offerup");
cap.setCapability("appActivity", "com.offerup.android.login.splash.LoginSplashActivity");
driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), cap);
}
#Test
public void SimpleTest() {
driver.findElement(By.id("email_button")).sendKeys("sourgta#gmail.com");
}
}
Try updated code

Trying to accept location permission pop up with Appium/Android 7/Java

I am using Appium 1.6.3 and a Google Pixel with Android 7.1.1.
I am trying to load the Waze app which is already installed on the phone and accept the initial location permission request.
I have tried the following:
capabilities.setCapability("autoGrantPermissions", "true");
Also:
capabilities.SetCapability("autoAcceptAlerts", true);
But these doesn't seem to do the trick.
I have read on other sites, that the auto granting of permission is only available on iOS?
The code I have currently:
import io.appium.java_client.MobileBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.html5.Location;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class mapTests {
AndroidDriver driver;
Dimension size;
String destDir;
DateFormat dateFormat;
#BeforeTest
public void setup() throws Exception{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "FA6C90301474");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1.1");
capabilities.setCapability("appPackage", "com.waze");
capabilities.setCapability("appActivity", "com.waze.FreeMapAppActivity");
capabilities.setCapability("noSign", true);
capabilities.setCapability("autoGrantPermissions", "true");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void testOne()throws Exception {
Location location = new Location(53.7775174,-1.800881200000049,224);
driver.setLocation(location);
takeScreenShot();
}
public void takeScreenShot() {
destDir = "screenshots";
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
dateFormat = new SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");
new File(destDir).mkdirs();
String destFile = dateFormat.format(new Date()) + ".png";
try {
FileUtils.copyFile(scrFile, new File(destDir + "/" + destFile));
} catch (IOException e) {
e.printStackTrace();
}
}
#AfterTest
public void tearDown() throws Exception{
driver.quit();
}
}
Thank you!
Actually, I have found an solution to my problem:
WebElement allow_location = driver.findElement(By.id("com.android.packageinstaller:id/permission_allow_button"));
allow_location.click();
Thread.sleep(1000);
This works a charm!
Try to use:
driver.switchTo().alert().accept();
or
driver.switchTo().alert().dismiss();
And don't forget to wait until the alert is present and then click on it.
This capabilities worked for me:
caps = {}
caps[“platformName”] = "Android"
caps[“platformVersion”] = "7.0"
caps[“deviceName”] = "Pixel_C_API_24"
caps[“automationName”] = "UiAutomator2"
caps[“app”] = "/home/tests/app-debug.apk"
caps[“noReset”] = False
caps[“autoGrantPermissions”] = True
caps[“appPackage”] = "com.mypackage"
caps[“appActivity”] = “.MyActivity”
Regards.
try to use the AndroidMobileCapabilityType.It will enable all permissions that apk requires.
capabilities.setCapability(AndroidMobileCapabilityType.AUTO_GRANT_PERMISSIONS, "true");

Appium-JAVA Error: unable to load class 'org.eclipse.ui.internal.ide.application.addons.ModelCleanupAddon' from bundle '731'

stacktrace screenshot I am trying to run different tests that I have written, through Appium on an Android device, but I am getting the following errors:
unable to load class 'org.eclipse.ui.internal.ide.application.addons.ModelCleanupAddon' from bundle '731'
or
unable to load class 'org.eclipse.ui.internal.ide.application.addons.ModelCleanupAddon' from bundle '561'
My code is:
import org.openqa.selenium.By;
//import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.WebElement;
//import org.openqa.selenium.remote.DesiredCapabilities;
//import org.openqa.selenium.remote.RemoteWebDriver;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
//import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;
//import java.util.concurrent.TimeUnit;
//import org.junit.Test;
public class CustomerLogin {
AppiumDriver driver;
takeScreenshot SC;
#BeforeClass
public void setUp() throws MalformedURLException{
File app = new File(System.getProperty("user.dir")+ "\\apks\\DropCarOwner-STAGE-v2.1.0-rc.1.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("DeviceName","306SH");
capabilities.setCapability("PlatformValue", "4.4.2");
capabilities.setCapability("PlatformName", "Android");
capabilities.setCapability("app",app.getAbsolutePath());
driver= new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
}*/
#Test
public void Login() throws Exception{
WebElement email= driver.findElement(By.id("com.dropcar.owner:id/editTextEmailAddress"));//com.dropcar.owner:id/editTextEmailAddress
email.sendKeys("a","w","a","i","s","d","u","r","r","a","n","i","8","7","#","g","m","a","i","l",".","c","o","m");
WebElement password= driver.findElement(By.id("com.dropcar.owner:id/editTextPassword")); //com.dropcar.owner:id/editTextPassword
password.sendKeys("c","h","e","c","k","i","n","g","1","2","3");
WebElement check= driver.findElement(By.name("Remember me")); //com.dropcar.owner:id/checkBoxRememberMe
driver.tap(0, check,0);
//check.click();
WebElement signIn=driver.findElement(By.name("SIGN IN"));
driver.tap(1,signIn,1);
//signIn.click();
SC.Screenshot(driver);
}
#AfterTest
public void end(){
driver.quit();
}
}
I am using Eclipse 4.4.1. How can I resolve this error?
Make sure before running the script appium-doctor command is working successfully. If you will attach the appium log then it will be more easy to tell you the actual solution of your problem.

Categories