Selenium + Java + Firefox + Windows is not working properly - java

I need help with following combination,
OS: Windows 10
Browser used :Firefox 45.0.1
Java version:Java 8 update 51( 64 bit)
Selenium : library 2.47.1
Our code is simple.
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.testng.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class TestClass01
{
static final Logger logger1 = LogManager.getLogger(TestClass01.class.getName());
WebDriver driver ;
String baseUrl ;
static int testCount = 0 ;
String[] content_heading ;
List<WebElement> temp_list ;
WebDriverWait wait;
boolean exists;
#BeforeClass
public void beforeClass()
{
logger1.entry();
logger1.info("Entering the class : " + this.getClass().getSimpleName() );
driver = new FirefoxDriver();
baseUrl = "http://www.google.com";
logger1.info("Maximizing the browser window and setting up the implicit timeout for element/page loading....");
driver.manage().window().maximize();
//Specifies the amount of time the driver should wait when searching for an element
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
logger1.info("Fetching the Homepage for Jacuzzi");
// launch Firefox and direct it to the Base URL
driver.get(baseUrl+"/");
}
}
However, it throws following error,
org.openqa.selenium.firefox.NotConnectedException: Unable to connect
to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
DEBUG Updating XPIState for
{"id":"{972ce4c6-7e08-4474-a285-3208198ce6fd}","syncGUID":"BbZlO30v46U7","location":"app-global","version":"45.0.1","type":"theme","internalName":"classic/1.0","updateURL":null,"updateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"icons":{"32":"icon.png","48":"icon.png"},"iconURL":null,"icon64URL":null,"defaultLocale":{"name":"Default","description":"The
default
theme.","creator":"Mozilla","homepageURL":null,"contributors":["Mozilla
Contributors"]},"visible":true,"active":true,"userDisabled":false,"appDisabled":false,"descriptor":"C:\Program
Files (x86)\Mozilla
Firefox\browser\extensions\{972ce4c6-7e08-4474-a285-3208198ce6fd}.xpi","installDate":1458533973089,"updateDate":1458533973089,"applyBackgroundUpdates":1,"skinnable":true,"size":22012,"sourceURI":null,"releaseNotesURI":null,"softDisabled":false,"foreignInstall":false,"hasBinaryComponents":false,"strictCompatibility":true,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"45.0.1","maxVersion":"45.0.1"}],"targetPlatforms":[],"seen":true}

This was happening because of new issue in latest browser of firefox.
Update your selenium jars. The new version of firefox(Or different browser) is not supporting old jars of selenium.
Download both Selenium Server (formerly the Selenium RC Server) Selenium Client & WebDriver Language Bindings
Replace them with old jars you are using. Update your mozilla also so you can get the updated results
source :- http://docs.seleniumhq.org/download/
To overcome from this issue you also need to setPreference as xpinstall.signatures.required", false to firefox Profile and then pass it to driver object
firefoxProfile.setPreference("xpinstall.signatures.required", false);
Below code is working fine for me.
static WebDriver driver=null;
public static void main(String[] args) {
final FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("xpinstall.signatures.required", false);
driver = new FirefoxDriver(firefoxProfile);
driver.get("https://www.google.de/");
Hope it will help you :)

Related

How to run a Java Selenuim test on a certain Chrome profielr [duplicate]

This question already exists:
How to run a Java Selenium test on an incognito Google Chrome profile
Closed 2 years ago.
I hope that you're fine. I'm new at Java Selenium automation, what I want to do is I want to run a test on a certain website but not in a clean session of ChromeDriver I would like to use a certain profile of mine I name it "test" its shortcut in the Desktop is "test.lnk", here what I did:
import static org.testng.Assert.assertEquals;
import org.openqa.selenium.By;
import org.openqa.selenium.By.ByXPath;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
//import org.openqa.selenium.edge.EdgeDriver;
import org.testng.annotations.Test;
public class AutomationTest {
WebDriver driver;
#BeforeTest
public void setUp() {
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data/test");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
// driver = new ChromeDriver();
driver.get("https://www.rapidtables.com/tools/click-counter.html");
driver.manage().window().maximize();
}
#Test
public void testRegister() throws InterruptedException {
do {
Thread.sleep(3000);
driver.findElement(By.id("addbtn")).click();
} while(true);
}
#AfterTest
public void tearDown() {
// driver.close();
}
}
```
chrom_options.add_argument("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data")
chrom_options.add_argument("profile-directory=test)
specify only the user data folder in userdata dir argument
Pass profile directory separately

How to use tor browser using selenium webdriver (java)? I have tried below code so far but getting message: 'tor failed to start'

import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
public class torr1 {
public static void main(String[] args) {
System.setProperty("webdriver.firefox.marionette",
"C:\\Users\\ghorh\\Documents\\selenium-bazinga\\Drivers\\geckodriver.exe");
String torPath = "C:\\Users\\ghorh\\Desktop\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "C:\\Users\\ghorh\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
File torProfileDir = new File(profilePath);
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
FirefoxOptions options = new FirefoxOptions();
options.setBinary(binary);
options.setProfile(torProfile);
options.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);
WebDriver driver = new FirefoxDriver(options);
driver.get("http://google.co.in");
}
}
I have tried the above code so far but getting message: 'tor failed to start'. Could somebody please help on what changes are required for the above code.
You are trying to use FireFox driver.
Try use TorBrowserDriver as specified in its readme: https://github.com/webfp/tor-browser-selenium
Or if you want to use firefox - use GeckoDriver that can be downloaded from the next link:
https://github.com/mozilla/geckodriver/releases/tag/v0.26.0

phantomjs not capturing screenshot from https website

I am trying to capture simple screenshot using phantomjs for https://www.google.com . Screenshot is coming as blank . I am using eclipse in windows and jars phantomjsdriver1.1 , selenium jars all 2.39 version .
When I mention site as http://google , it redirects to https and captures screenshot . But I need it to exclusively capture it for https only . Below is my code . Thanks in advance .
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import net.anthavio.phanbedder.Phanbedder;
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.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Schedule {
public static void main(String[] args) {
File phantomjs = Phanbedder.unpack();
DesiredCapabilities dcaps = new DesiredCapabilities();
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());
dcaps.setCapability("takesScreenshot", true);
String [] phantomJsArgs = {"---ignore-ssl-errors=yes"};
dcaps.setCapability(
PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS,
phantomJsArgs);
PhantomJSDriver driver = new PhantomJSDriver(dcaps);
driver.get("https://www.google.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile, new File("C:\\Users\\Desktop\\fci\\sample.jpeg"),true);
} catch (IOException e) {
System.out.println("exception");
}
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
PhantomJS is a headless browser meaning; you don't get to see what is happening; everything that's done is done in background.

Headless browser not executing properly

I use selenium webdriver to automate my test-cases.
My objective to execute headless browser using HtmlUnitDriver on a sample selenium script. Please find the script mentioned below:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Headless
{
public static void main(String[] args) throws InterruptedException
{
HtmlUnitDriver driver = new HtmlUnitDriver();
//driver.setJavascriptEnabled(true);
// WebDriver driver=new FirefoxDriver();
driver.get("https://www.google.co.in/?gfe_rd=cr&ei=k36cVsa6OubI8Aec14bICQ&gws_rd=ssl");
/*WebDriverWait wait=new WebDriverWait(driver,120);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("sb_ifc0")));
*/
Thread.sleep(50000);
System.out.println("URL= "+driver.getCurrentUrl());
System.out.println("Page title is: " + driver.getTitle());
}
}
And the output is:
URL= about:blank
Page title is:
The output is working fine for FirefoxDriver()
Could anyone guide where I went wrong?

How to run appium script in remote machine?

I have 2 desktop machines in which Appium script is saved in machine 1 and Appium got installed in machine 2 and android devices also connected in machine 2. now i want to take the script from the machine 1 and execute it in the devices connected in the machine 2. how to achieve this. please suggest me.
The below code i am able to run same machine.i want to run on android device which is connected in another machine.Both machine ip segment is same segment.
package com.appiumproj.test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
public class Appium {
AppiumDriver driver;
#BeforeClass
public void setUp() throws MalformedURLException{
//Set up desired capabilities and pass the Android app-activity and app-package to Appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.VERSION, "5.0.2");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"ZX1D62FPVQ");
capabilities.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.calculatord");
capabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.calculator2.Calculator");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void testCal(){
WebElement two=driver.findElement(By.name("2"));
two.click();
WebElement plus=driver.findElement(By.name("+"));
plus.click();
WebElement four=driver.findElement(By.name("4"));
four.click();
WebElement equalTo=driver.findElement(By.name("="));
equalTo.click();
WebElement results=driver.findElement(By.className("android.widget.EditText"));
assert results.getText().equals("6"):"Actual value is : "+results.getText()+" did not match with expected value: 6";
System.out.println("Inside Test Function");
terClass
public void teardown(){
driver.closeApp();
}
}
package com.appiumproj.test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
public class Appium {
AppiumDriver driver;
#BeforeClass
public void setUp() throws MalformedURLException{
//Set up desired capabilities and pass the Android app-activity and app-package to Appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.VERSION, "5.0.2");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"ZX1D62FPVQ");
capabilities.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.calculatord"); // This is package name of your app (you can get it from apk info app
capabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.calculator2.Calculator"); // This is Launcher activity of your app (you can get it from apk info app)
//Create AndroidDriver instance and connect to the Appium server.
//It will launch the Calculator App in Android Device using the configurations specified in Desired Capabilities
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void testCal(){
//locate the Text on the calculator by using By.name()
WebElement two=driver.findElement(By.name("2"));
two.click();
WebElement plus=driver.findElement(By.name("+"));
plus.click();
WebElement four=driver.findElement(By.name("4"));
four.click();
WebElement equalTo=driver.findElement(By.name("="));
equalTo.click();
//locate the edit box of the calculator by using By.className()
WebElement results=driver.findElement(By.className("android.widget.EditText"));
//Check the calculated value on the edit box
assert results.getText().equals("6"):"Actual value is : "+results.getText()+" did not match with expected value: 6";
/*
System.out.println("Inside Test Function");
driver.findElement(By.partialLinkText("More")).click();
driver.findElement(By.xpath("//EditText[#text='Email Address']")).sendKeys("tester#gmail.com");
driver.findElement(By.xpath("//LinearLayout/EditText[2]")).sendKeys("Testerpwd");
driver.findElement(By.xpath("//CheckBox")).click();
driver.findElement(By.xpath("//Button[#text='Login']")).click();
WebDriverWait wait = new WebDriverWait(driver,80);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//Button[#text='Logout']")));
driver.findElement(By.xpath("//Button[#text='Logout']")).click();
*/
}
#AfterClass
public void teardown(){
//close the app
driver.closeApp();
}
}
Give the exact Ip address on the Appium Instance running on Machine 2. By default it is 127.0.0.1, remove this and give its own IP address explicitly in Appium settings. This will work. Let me know if not.

Categories