Can I open 2 Firefox Drivers on 2 Screens? - java

I am running tests using two Firefox Drivers to display the results. I am using two screens on my computer. Can I automatically open a driver on each screen via java code running in Eclipse?
Thanks.

I think you should run parallel test cases https://saucelabs.com/java/se2/8
#RunWith(Parallelized.class)
public class WebDriverParallelTest {
private String browser;
private String os;
private String version;
public WebDriverParallelTest(String os, String version, String browser) {
super();
this.os = os;
this.version = version;
this.browser = browser;
}
#Parameterized.Parameters
public static LinkedList browsersStrings() throws Exception {
LinkedList browsers = new LinkedList();
browsers.add(new String[]{Platform.XP.toString(), "17", "firefox"});
//add any additional browsers here
return browsers;
}
private WebDriver driver;
#Before
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, browser);
capabilities.setCapability(CapabilityType.VERSION, version);
capabilities.setCapability(CapabilityType.PLATFORM, os);
this.driver = new RemoteWebDriver(
new URL("http://credential of sauce lab"), capabilities);
}
#Test
public void webDriver() throws Exception {
driver.get("http://www.amazon.com/");
assertEquals("Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more", driver.getTitle());
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}

Related

Not able to click on permission pop-up (Access Contact List) in appium

I am new to Appium.
I am trying to automate an application which on launch asks to allow to access Contact List. I did research on various websites for the solution but couldn`t resole this issue.
Code:
public class TestSuiteBase {
WebDriver driver = null;
//Element Variables
String deviceIdTextbox = "et_Registration_DeviceID";
#BeforeClass
public void setup() throws MalformedURLException, InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "4.4.2");
capabilities.setCapability("deviceName", "0ef3c26f");
capabilities.setCapability("browserName", "");
capabilities.setCapability("app", "D:\\Ashish\\AppiumProject\\Mobile Framework\\apk\\androidApplication.apk");
capabilities.setCapability("appPackage", "com.atyati.rbl.mfi");
capabilities.setCapability("appActivity", "com.atyati.rbl.mfi.Activity.SplashScreenActivity");
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
Thread.sleep(10000);
}
#Test
public void Test() throws Exception {
Thread.sleep(2000);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("appWaitPackage", "com.android.packageinstaller");
capabilities.setCapability("appWaitActivity", ".permission.ui.GrantPermissionsActivity");
System.out.println("Trying to click Allow");
driver.findElement(MobileBy.id("permission_allow_button")).click();
driver.findElement(By.id(deviceIdTextbox)).sendKeys("123456789");
}
#AfterClass
public void tearDown() throws Exception {
driver.quit();
}
Output: My code launches the app but when permission pop-up surfaces, it is unable to click.
Issue: Not able click on permission pop-up.
Query: Do I need to specify "appActivity" before moving to each screen?
Let me know if any other details needed.
I have figured it out where I was going wrong.
Solution: I have to specify the complete resource id for the Edit Textbox
String deviceIdTextbox = "com.atyati.rbl.mfi:id/et_Registration_DeviceID";
Also, it is not needed to specify the activity.
#Test
public void Test() throws Exception {
Thread.sleep(1000);
if(androidDriver!=null){
System.out.println("driver does not == null");
System.out.println("Trying to click Allow");
androidDriver.findElement(By.id(contactListAllowPermissionBtn)).click();
Thread.sleep(1000);
androidDriver.findElement(By.id(deviceIdTextbox)).sendKeys("123456789");
androidDriver.findElement(By.id(registerUserBtn)).click();
} else {
System.out.println("driver == null");
}
}
You can use this JAVA CODE for clicking on allow app permission button as many times it appears. just add the method and in your class file and call the method in your test case. This code is taken from example on THIS POST
public void allowAppPermission(){
while (driver.findElements(MobileBy.xpath("//*[#class='android.widget.Button'][2]")).size()>0)
{ driver.findElement(MobileBy.xpath("//*[#class='android.widget.Button'][2]")).click();
}
}

selenium script unable to run

WebDriver driver;
String baseUrl = "http://example.com";
#BeforeClass
public void beforeClass() {
System.setProperty("webdriver.firefox.marionette","local path to geckodriver-v0.10.0-win64");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
driver = new FirefoxDriver();
}
#Test
public void login() throws InterruptedException {
driver.get(baseUrl);
Thread.sleep(3000);
}
#AfterClass
public void afterClass() {
}
this code I am trying to run using TestNG tool but it's failing.
It runs perfectly when using it as Java Application but I want to run this script as a TestNG script

How do I pass the current capabilities to individual tests

This may be as simple as getting a variable from another class. I am still learning Java and Selenium.
I would like the test run report (ExtentReports) to be able to report the browser at a #Test level (capabilities). Currently Grid runs the same tests on different browsers, and the report does not distinguish them.
Using Selenium Grid, I define my #Test's Capabilities (including browser) with #BeforeMethod. I do this in my BaseTest class.
public class BaseTest {
#BeforeMethod(alwaysRun = true)
#Parameters({ "platform", "browser", "version" })
public void setup(String platform, String browser, String version)
throws MalformedURLException, InterruptedException {
RemoteWebDriver driver = null;
//important: Thread local!
threadedDriver = new ThreadLocal<RemoteWebDriver>();
DesiredCapabilities caps = new DesiredCapabilities();
// Platforms
if (platform.equalsIgnoreCase("windows"))
caps.setPlatform(Platform.WINDOWS);
if (platform.equalsIgnoreCase("XP"))
caps.setPlatform(Platform.XP);
if (platform.equalsIgnoreCase("WIN8"))
caps.setPlatform(Platform.WIN8);
if (platform.equalsIgnoreCase("WIN8_1"))
caps.setPlatform(Platform.WIN8_1);
if (platform.equalsIgnoreCase("ANY"))
caps.setPlatform(Platform.ANY);
if (platform.equalsIgnoreCase("MAC"))
caps.setPlatform(Platform.MAC);
if (platform.equalsIgnoreCase("Android"))
caps.setPlatform(Platform.ANDROID);
// Browsers
if (browser.equalsIgnoreCase("Internet Explorer"))
caps.setBrowserName("internet explorer");
if (browser.equalsIgnoreCase("Firefox"))
caps.setBrowserName("firefox");
if (browser.equalsIgnoreCase("chrome"))
caps.setBrowserName("chrome");
if (browser.equalsIgnoreCase("MicrosoftEdge"))
caps.setBrowserName("MicrosoftEdge");
if (browser.equalsIgnoreCase("iPad"))
caps.setBrowserName("ipad");
if (browser.equalsIgnoreCase("iPhone"))
caps.setBrowserName("iphone");
if (browser.equalsIgnoreCase("Android"))
caps.setBrowserName("android");
// Version
caps.setVersion(version);
System.out.println(caps);
System.out.println(browser);
//Initialize driver with capabilities
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps);
//this uses below methods to set above RemoteWebDriver to the getDriver()
//method in a threaded instance.
setWebDriver(driver);
initialize();
}
}
So now I have a browser variable local to each threaded Grid test run. I need to get that variable into each #Test method. Here is my #Test in a separate class. At the beginning of the try statement I would like to print the browser variable for the current threaded Grid test capabilities
public class Workflow1 extends BaseTest {
#Test
public void Workflow1TestInvalidPolicyNumbers() throws InterruptedException {
HomePage homePage = new HomePage(getDriver());
ExtentTest testReporter = ComplexReportFactory.getTest();
try {
System.out.println("This is the browser:" + ??(help here)??);
loginMethod("TestUser","TestPassword");
homePage.setFindAPersonOrPolicySearchField("1234");
homePage.clickSearchButton();
testReporter.log(LogStatus.INFO, "Searched \"1234\"");
Thread.sleep(2000);
if (getDriver().getPageSource().contains("Policy numbers should be 7 or 10 digits long"))
testReporter.log(LogStatus.PASS, "Policy numbers should be 7 or 10 digits long");
else
testReporter.log(LogStatus.FAIL, "Results incorrect" + testReporter.addScreenCapture(ComplexReportFactory.CaptureScreen(getDriver())));
} catch (Exception e) {
testReporter.log(LogStatus.ERROR, "Exception found: " + e.getMessage()
+ testReporter.addScreenCapture(ComplexReportFactory.CaptureScreen(getDriver())));
System.out.println(e);
}
}
It looks like the browser information is passed into the BaseTest.java class in the setup() method.
You could store this data in a variable which would then be available to all dependant classes:
public class BaseTest {
protected String browser; // add a property to hold this value
#BeforeMethod(alwaysRun = true)
#Parameters({ "platform", "browser", "version" })
public void setup(String platform, String browser, String version)
throws MalformedURLException, InterruptedException {
this.browser = browser; // store the given browser string
RemoteWebDriver driver = null;
//important: Thread local!
threadedDriver = new ThreadLocal<RemoteWebDriver>();
DesiredCapabilities caps = new DesiredCapabilities();
// Platforms
if (platform.equalsIgnoreCase("windows"))
caps.setPlatform(Platform.WINDOWS);
And then the BaseTest subclasses could reference it directly:
public class Workflow1 extends BaseTest {
#Test
public void Workflow1TestInvalidPolicyNumbers() throws InterruptedException {
HomePage homePage = new HomePage(getDriver());
ExtentTest testReporter = ComplexReportFactory.getTest();
try {
System.out.println("This is the browser:" + browser); // then retrieve it
You can also get the browser flavor name and a whole bunch of information by querying the RemoteWebDriver object itself to reveal the actual capabilities by invoking
getDriver().getCapabilities().getBrowserName()
This does away with the need to even have the browser flavor as a separate data member in the test class.
See here for javadocs.

phantomjs cannot open url

I am not having any luck getting Phantomjs to access a url in my Selenium webdriver tests. I continue to get the following error after the assert -
Expected :Login
Actual :403 - Forbidden: Access is denied.
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at
Here is my - Also HTML Unit Driver does the same thing. However when I use Firefox driver it runs fine.
public class EasyElectPhantomTest {
private WebDriver driver;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
private String baseUrl;
#Before
public void setUp() throws Exception {
driver = new PhantomJSDriver();
baseUrl = "https://secure02.pilot.principal.com/";
#Test
public void testEasyElectPhantom() throws Exception {
driver.get(baseUrl + "/login?token=ODEFIJMLEHGIOPRNGIYN");
driver.manage().window().maximize(); //Maximizing window
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
assertEquals("Login", driver.getTitle());
driver.findElement(By.id("firstName")).sendKeys("Ryan");

Selenium WebDriver using TestNG and Excel

Hello i really need help with Selenium WebDriver using TestNG and Excel
i try to get data from excel to open browser and navigate URL. its work successfully and terminal and testng report showing test pass but its not open browser or doing anything its just run its self and show report
Config File
public void openBrowser(String browser){
try {
if (browser.equals("Mozilla")) {
driver = new FirefoxDriver();
} else if(browser.equals("IE")){
driver = new InternetExplorerDriver();
} else if(browser.equals("Chrome")){
System.setProperty("webdriver.chrome.driver", "\\Applications\\Google Chrome.app\\Contents\\MacOS\\Google Chrome ");
driver = new ChromeDriver();
}
} catch (Exception e) {
}
}
public void navigate(String baseUrl){
try {
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.navigate().to(baseUrl);
} catch (Exception e) {
}
}
And Test Execute File
public class NewTest {
public String exPath = Config.filePath;
public String exName = Config.fileName;
public String exWrSheet = "Logiin Functional Test";
public Config config;
#BeforeTest
public void openBrowser() {
config = new Config();
Excel.setExcelFile(exPath+exName, exWrSheet);
String browser = Excel.getCellData(1, 2);
config.openBrowser(browser);
}
#BeforeMethod
public void navigate() {
config = new Config();
Excel.setExcelFile(exPath+exName, exWrSheet);
String baseUrl = Excel.getCellData(2, 2);
config.navigate(baseUrl);
}
#Test
public void test() {
System.out.println("Test");
}
#AfterTest
public void closeBroser() {
//Config.tearDown();
}
I don't have quite enough rep to make a comment, which I would prefer here, but if you aren't getting any sort of exception, I'd suspect the value you're getting for the browser variable is not matching anything in your if-then-else in openBrowser and then falling through. Step through the code with a debugger or just add:
String browser = Excel.getCellData(1, 2);
System.out.println("Browser value from Excel =" + browser);
config.openBrowser(browser);
to see what you're reading from the file.
1 - TestNg is always pass because you are using "void" method and you catch "all" exception
2 - No browser opened because in openBrowser(String browser), NullPointException throws and you already catch it.
-> you need to init an instance of WebDriver and pass it through your test.

Categories