PhantomJSDriver works for HTTP but not for HTTPS - java

public class FooTest {
WebDriver driver;
#Before
public void beforeTest() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setJavascriptEnabled(true);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver = new PhantomJSDriver(capabilities);
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
}
#Test
public void test() {
driver.get("http://www.example.com");
WebElement e = driver.findElement(By.tagName("h1"));
System.out.println("TEXT" + e.getAttribute("innerHTML"));
assertNotNull(e);
driver.quit();
}
}
Hi, I'm just simply trying get the h1 tag in www.example.com which says "Example Domain." The code works for http://www.example.com but not for https://www.exmaple.com. How can I fix this problem? Thanks

PhantomJSDriver does not support (all) DesiredCapabilities.
You will need:
caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--web-security=no", "--ignore-ssl-errors=yes"});
driver = new PhantomJSDriver(caps);
Documented here: https://github.com/detro/ghostdriver/issues/233

Related

Unable to drag and drop in selenium for only this website

WebDriver driver = new ChromeDriver(); //Launch the chrome browser
driver.get("https://www.seleniumeasy.com/test/drag-and-drop-demo.html");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[#id=\"todrag\"]/span[2]"));
WebElement from = driver.findElement(By.xpath("//*[#id=\"todrag\"]/span[2]"));
WebElement to = driver.findElement(By.xpath("//*[#id='mydropzone']"));
Actions builder = new Actions(driver);
builder.dragAndDrop(from, to).perform();
I put it in Junit test. Works fine for me.
But I noticed you're not setting property to use a local chromedriver.
Have you got a chromedriver downloaded?
private WebDriver driver;
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver",
//"C:/path/to/your/chromedriver.exe"
"/path/to/your/chromedriver"); // Might be this.
driver= new ChromeDriver();
}
#After
public void tearDown() throws Exception {
driver.quit();
}
#Test
public void testDragNDrop() {
driver.get("https://www.seleniumeasy.com/test/drag-and-drop-demo.html");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[#id=\"todrag\"]/span[2]"));
WebElement from = driver.findElement(By.xpath("//*[#id=\"todrag\"]/span[2]"));
WebElement to = driver.findElement(By.xpath("//*[#id='mydropzone']"));
Actions builder = new Actions(driver);
builder.dragAndDrop(from, to).perform();
}

how to Make selectable appPackage and appActivity BeforeScenario in android Appium Test Automation

I want to choose starting app(appPackage,appActivity) in #BeforeScenario to develop a test scenarios for multiple apps in one test project. I know use the start activity but because of security permission denial, I can not use it. The only working method is using capabilities in beforeScenario. Before start the test I want to choose the starting app.My Code:
`#BeforeScenario
public void beforeScenario() throws MalformedURLException {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities
.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "device");
desiredCapabilities.setCapability(MobileCapabilityType.UDID, "L2N4C19924005752");
if (localAndroid) {
logger.info("Local Browser");
desiredCapabilities
.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,
notapadAppPackageName);
desiredCapabilities
.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,
notepadAppActivityName);
}
desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
desiredCapabilities.setCapability(MobileCapabilityType.NO_RESET, true);
desiredCapabilities.setCapability(MobileCapabilityType.FULL_RESET, false);
desiredCapabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 300);
desiredCapabilities.setCapability("unicodeKeyboard", false);
desiredCapabilities.setCapability("appWaitDuration", 30);
desiredCapabilities.setCapability("resetKeyboard", false);
URL url = new URL("http://localhost:4723/wd/hub");
appiumDriver = new AndroidDriver(url, desiredCapabilities);
selector = SelectorFactory
.createElementHelper(localAndroid ? SelectorType.ANDROID : SelectorType.IOS);
appiumDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
appiumFluentWait = new FluentWait(appiumDriver);
appiumFluentWait.withTimeout(8, TimeUnit.SECONDS)
.pollingEvery(350, TimeUnit.MILLISECONDS)
.ignoring(NoSuchElementException.class);
}
`
You can pass app name from VM aguements and base on that you can set driver instance. Please have a look on code below:
#BeforeScenario public void beforeScenario(String appName) throws MalformedURLException {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
switch (appName) {
case "notepadApp":
desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,notapadAppPackageName);
desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,notepadAppActivityName);
break;
case "App-A":
desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,appAAppPackageName);
desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,appApadAppActivityName);
break;
case "App-B":
desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,appBAppPackageName);
desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,appBApadAppActivityName);
break;
default:
System.out.println("No matching app is found.");
}
// Common desired capabilities set
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "device");
desiredCapabilities.setCapability(MobileCapabilityType.UDID, "L2N4C19924005752");
desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
desiredCapabilities.setCapability(MobileCapabilityType.NO_RESET, true);
desiredCapabilities.setCapability(MobileCapabilityType.FULL_RESET, false);
desiredCapabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 300);
desiredCapabilities.setCapability("unicodeKeyboard", false);
desiredCapabilities.setCapability("appWaitDuration", 30);
desiredCapabilities.setCapability("resetKeyboard", false);
URL url = new URL("http://localhost:4723/wd/hub");
appiumDriver = new AndroidDriver(url, desiredCapabilities);
selector = SelectorFactory
.createElementHelper(localAndroid ? SelectorType.ANDROID : SelectorType.IOS);
appiumDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
appiumFluentWait = new FluentWait(appiumDriver);
appiumFluentWait.withTimeout(8, TimeUnit.SECONDS)
.pollingEvery(350, TimeUnit.MILLISECONDS)
.ignoring(NoSuchElementException.class);
}

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.

Can't connect my 'BrowserFactory' to 'Selenium Grid'

I can't connect my BrowserFactory to Selenium Grid. Any ideas why the following code won't work?
public static WebDriver getDriver() throws Exception {
try {
// Load the driver selected by user
Properties p = new Properties();
FileInputStream fi = new FileInputStream(Constant.CONFIG_PROPERTIES_DIRECTORY);
p.load(fi);
if(p.getProperty("use_grid").equalsIgnoreCase("true")) {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.getBrowserName();
desiredCapabilities.setPlatform(Platform.WINDOWS);
return new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), desiredCapabilities);
}
String browserName = p.getProperty("browser");
switch (browserName) {
case "firefox":
if (null == webdriver) {
System.setProperty("webdriver.gecko.driver", Constant.GECKO_DRIVER_DIRECTORY);
webdriver = new FirefoxDriver();
}
break;
I have the node and grid up and running successfully.
Thanks.
It looks like you're not setting the browser when using selenium grid. Try changing this line:
desiredCapabilities.getBrowserName();
to this:
desiredCapabilities.setBrowserName("firefox");
or this if you're properties are in the correct format and that code is working correctly:
desiredCapabilities.setBrowserName(p.getProperty("browser"));

Categories