unable to launch internet explorer in TestNG - selenium-webdriver

I am new in TestNG. This is my code which I tried in eclipse, but there is a problem occurring in launching internet explorer.
The error it is giving is
org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (WARNING: The server did not provide any stacktrace information)
This is the complete code....
package com.tcs.medmantra;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Registration {
WebDriver driver;
WebElement element;
WebElement element2;
WebDriverWait waiter;
#Test(priority = 1)
public void register_With_Cash() throws RowsExceededException, BiffException, WriteException, IOException
{
driver=new InternetExplorerDriver();
driver.get("https://172.25.155.250/loginpage.aspx");
//((JavascriptExecutor)driver).executeScript("window.resizeTo(1366, 768);");
waiter = new WebDriverWait (driver, 40);
driver.findElement(By.id("txtuname")).sendKeys("122337");
driver.findElement(By.name("txtpwd")).sendKeys("Tcs!#345");
driver.findElement(By.id("btnsubmit")).click();
sleep(25000);
//print URL
String url = driver.getCurrentUrl();
System.out.println(url);
}
#BeforeTest
public void beforeTest() {
File file = new File("D:\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
}
#AfterTest
public void afterTest() {
driver.quit();
}
}

As the error says you need set the protected mode same for all zones, either enabled or disabled. Preferred would be enabled. See here

Change your code to include the path for InternetExplorerDriver.
File file = new File("C:/Selenium/iexploredriver.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();

Related

Why getting resource list null for webpage when automating desktop application i.e. hybrid (Desktop and Web)?

I am automating hybrid application i.e. Desktop and WebApplication. We have desktop application in which some of pages are integrated using WebPages and some one pages are in desktop application. Below is the code which I have used to open Desktop Application. And its opening the application successfully. But When I am going to print resource list size, it comes 'null' where locator for resourcelist is showing correct count in chrome browser. I can login successfully using winium driver instance. After logged in, list of resources are present which I integrated in webpage.
Let me know if I am doing wrong in given code?
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.winium.DesktopOptions;
import org.openqa.selenium.winium.WiniumDriver;
import org.openqa.selenium.winium.WiniumDriverService;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
public class VMSWiniumBaseDriver {
public WiniumDriver driver;
String applicationPath = "C:\\Users\\prashantn\\Downloads\\Publish\\ABC.exe";
#Parameters({ "windowsPlatform" })
#BeforeClass(alwaysRun = true)
public void initialize(String browser) throws IOException, InterruptedException {
if (browser.equalsIgnoreCase("desktop")) {
try {
WiniumDriverService service;
DesktopOptions option;
option = new DesktopOptions();
option.setApplicationPath(applicationPath);
File driverPath = new File(System.getProperty("user.dir") + File.separator + "driver" + File.separator
+ "Winium.Desktop.Driver.exe");
service = new WiniumDriverService.Builder().usingDriverExecutable(driverPath).usingPort(9999)
.withVerbose(true).withSilent(false).buildDesktopService();
try {
service.start();
} catch (IOException e) {
System.out.println("Exception while starting WINIUM service");
e.printStackTrace();
}
driver = new WiniumDriver(service, option);
} catch (Exception e) {
System.out.println(e);
}
}
}
#AfterClass(alwaysRun = true)
public void TeardownTest() {
if (null == driver) {
driver.close();
driver.quit();
}
}
public WiniumDriver getDriver() {
return driver;
}
}
Here is
CommonUtilities.Java
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.concurrent.ThreadLocalRandom;
import org.openqa.selenium.WebDriver;
import org.sikuli.hotkey.Keys;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
import org.sikuli.script.ScreenImage;
import dataproviders.VMSConfigFileReader;
import managers.VMSWiniumBaseDriver;
public class CommonUtilities extends VMSWiniumBaseDriver {
public WebDriver getWebDriverInstance() {
WebDriver webDriver = (WebDriver) driver;
return webDriver;
}
}
LoginPage.java
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.winium.WiniumDriver;
import utilities.CommonUtilities;
public class LoginPage{
#FindBy(id = "PART_EditableTextBox")
WebElement userName;
#FindBy(id = "TextBlock_Password")
WebElement passwordInput;
#FindBy(id = "LoginButton")
WebElement loginButton;
#FindBy(xpath = "//dd[contains(#class,'vms-tree-enabled')]/span[#class='vms-treeview-label'][#title!='Removed channels'][#title!='Unassociated']")
List<WebElement> resourcesList;
public void getResourcesList() {
CommonUtilities commonUtilities = new CommonUtilities();
// System.out.println("Page Source :: "+commonUtilities.getWebDriverInstance());
List<WebElement> list = commonUtilities.getWebDriverInstance().findElements(By.xpath(
"//dd[contains(#class,'vms-tree-enabled')]/span[#class='vms-treeview-label'][#title!='Removed channels'][#title!='Unassociated']"));
System.out.println("Resources Size :: " + list.size());
for (int i = 0; i < resourcesList.size(); i++) {
System.out.println("Resource Name :: " + resourcesList.get(i).getText());
}
}
}
Exception Log:
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.findElements(org.openqa.selenium.By)" because the return value of "utilities.CommonUtilities.getWebDriverInstance()" is null
at pageobjects.LoginPage.getResourcesList(LoginPage.java:57)
at testcases.AppLaunchTest.selectLayOutBasic1(AppLaunchTest.java:62)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:599)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:822)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.testng.TestRunner.privateRun(TestRunner.java:764)
at org.testng.TestRunner.run(TestRunner.java:585)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1218)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.runSuites(TestNG.java:1069)
at org.testng.TestNG.run(TestNG.java:1037)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

Test annotation code is not working, just BeforeMethod is working

My code is working until #BeforeMethod and it is opening the URL, but what I'm typing after #Test is not working. It is not typing credentials on the login page, hence Test failed.
package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
// import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Great {
WebDriver driver;
#BeforeMethod
public void setup()
{
System.setProperty("webdriver.chrome.driver", "C:\\Program Files
(x86)\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseurl = "https://getfieldforce.com/dishqa";
driver.get(baseurl);
// Actions act = new Actions(driver);
}
#Test (priority=0)
public void login()
{
System.out.println("Login process starts");
driver.findElement(By.id("email")).sendKeys("123#p.com");
driver.findElement(By.id("password")).sendKeys("123456");
driver.findElement(By.id("cta")).click();
System.out.println("Login Sucessfully");
}
Removing webdriver from this line fixed the issue; WebDriver driver = new ChromeDriver();
Thanks for looking into this :-)

Openbravo ERP application using selenium webdriver but not able to find any web element

When I'm trying to run this code, I am unable to find any web element
package PremisesManagement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
public class openbravo {
#Test
public void openbravoLoginTest() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
WebDriver d = new ChromeDriver();
d.get("http://119.81.222.91:8080/camps/security/Login_FS.html");
Thread.sleep(3000);
d.findElement(By.id("user")).sendKeys("xyz");
WebDriverWait wait = new WebDriverWait(d, 120);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("//button[#id='buttonOK']")));
d.findElement(By.xpath("//button[#id='buttonOK']")).click();
}
}
Actually located elements are inside a frame with id paramFrame1, So you need to switch this frame before finding element as below :-
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://119.81.222.91:8080/camps/security/Login_FS.html");
WebDriverWait wait = new WebDriverWait(driver, 10);
//First switch to frame with id paramFrame1
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("paramFrame1"));
//Now find the element
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("user"))).sendKeys("xyz");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("buttonOK"))).click();

Null Pointer Exception using TestNG

I am trying to automate and have integrated Selenium with Appium and I am using Eclipse with TestNG for execution.
Now within a project I have a package with two classes; the first class is for login into the App and the second class is for logout
First class code -
package com.gma.test;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class LoginApp {
public AppiumDriver driver;
#BeforeSuite
public void beforeMethod() throws InterruptedException {
File app = new File("C:\\Users\\mc30058\\Downloads\\gma-QA-RELEASE-QRCode-07022015_v4.5.4028.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","Android Emulator");
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("app", app.getAbsolutePath());
try {
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.xpath("//android.widget.Button[#resource-id='com.mcdonalds.app:id/button_choose_closest']")).click();
driver.findElement(By.xpath("//android.widget.Button[#text='Continue']")).click();
driver.findElement(By.xpath("//android.widget.Button[#text='OK']")).click();
}
#Test (priority=1)
public void Login() {
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.xpath("//android.widget.TextView[#text='Sign In']")).click();
driver.findElement(By.xpath("//android.widget.EditText[#resource-id='com.mcdonalds.app:id/signin_edittext_email']")).sendKeys("anuj.shrivastava#us.mcd.com");
driver.findElement(By.xpath("//android.widget.EditText[#resource-id='com.mcdonalds.app:id/signin_edittext_password']")).sendKeys("Anujtest2");
currentTime();
driver.findElement(By.xpath("//android.widget.Button[#text='Sign In']")).click();
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.xpath("//android.widget.Button[#text='Yeah, count me in!']")).isDisplayed();
currentTime();
driver.findElement(By.xpath("//android.widget.Button[#text='No, thanks. I like to be out of the loop']")).click();
}
private void currentTime() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println( sdf.format(cal.getTime()) );
}
}
Second class code:
package com.gma.test;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Logout {
public AppiumDriver driver;
#Test (priority=3)
public void LogoutApp() {
driver.findElement(By.xpath("//android.widget.TextView[#content-desc='Navigation Button']")).click();
driver.findElement(By.xpath("//android.widget.TextView[#text='Sign Out']")).click();
driver.findElement(By.xpath("//android.widget.Button[#text='Sign Out']")).click();
}
}
Contents from Testng.xml
<suite name = "GMA Automation">
<test name = "GMA Automation">
<classes>
<class name = "com.gma.test.LoginApp"/>
<class name = "com.gma.test.Logout"/>
</classes>
</test>
</suite>
I receive Null Pointer exception after execution of first class. Appium server stops saying no more commands received. Please help.
You are getting NPE b'coz you have declared one more driver reference public AppiumDriver driver; in your Logout class.
And you are using this driver reference without instantiation.
In logout class you forget to instantiate your AppiumDriver driver.
try this:
public AppiumDriver driver;
driver = new AndroidDriver();

autosuggest textbox using webdriver java

I'm i\using webdriver version2.41 and browser is Firefox 28. I'm trying to find a listcount of elements present in the drop down list of a auto suggest textbox.Ex: in Google.co.in page i'm writing Banga to get the suggestions for Bangalore. Once i get the suggestion list then i want to dispay all the Auto suggested text on the screen. I have written the code, but don't know why its not working. I'm anew bie to selenium webdriver. Please help me. Here is my code :
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class test {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.id("gbqfq")).sendKeys("Banga");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
List<WebElement> lstobj = driver.findElements(By.xpath("//div[#class='gsq_a']/table/tbody/tr/td/span/b"));
System.out.println(lstobj.size());
for (int i = 0; i<lstobj.size();i++)
{
String p= lstobj.get(i).getText();
System.out.println(p);
}
}
}
I hope this helps u..
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class google{
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.id("gbqfq")).sendKeys("Banga");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
WebElement parent=driver.findElement(By.className("gssb_e"));
List<WebElement> child = parent.findElements(By.tagName("div"));
int size=child.size();
System.out.println(size);
for (int i =1; i<=size;i++)
{
String p= driver.findElement(By.xpath("//*[#id='gsr']/table/tbody/tr[1]/td[2]/table/tbody/tr["+i+"]/td/div/table/tbody/tr/td[1]")).getText();
System.out.println(p);
driver.close();
}
}
}
Edited the xpath used by you and the way you retrieved text :
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class test {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.id("gbqfq")).sendKeys("Banga");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
List<WebElement> lstobj = driver.findElements(By.xpath("//table[#class='gssb_m']/tbody/tr"));
System.out.println(lstobj.size());
for (int i = 0; i<lstobj.size();i++)
{
String p= lstobj.get(i).findElement(By.xpath("//span")).getText();
System.out.println(p);
}
}
driver.get("https://www.google.co.in/");
driver.findElement(By.xpath("//input[#class='gLFyf gsfi']")).sendKeys("Banga");
//This will also work USE descendant to get all child element
List<WebElement> printlist = driver.findElements(By.xpath("//ul[#role='listbox']//li/descendant::div[#class='sbl1']"));
System.out.println(printlist.size());
for ( WebElement list: printlist) {
//if you want to specify condition here you can
System.out.println(list.getText());
}

Resources