Unable to find the element on emulator screen of Android app - automation

The element appears within the appium view but unfortunately the element does not seems visible while automating it.
private AndroidDriver driver;
String idOfCNIC = "com.tez.androidapp:id/imageViewNICDetails";
this.driver.findElement(By.id(idOfCNIC))`
Following is the driver initialization code :
private static AndroidDriver driver;
public static AndroidDriver getDriver() {
if (driver == null) {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "920121cb3c7fc34a");
caps.setCapability("platformName", "Android");
caps.setCapability(CapabilityType.VERSION, "6.0.1");
caps.setCapability(CapabilityType.BROWSER_NAME, "Android");
caps.setCapability("app", "path_of_app");
(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
try {
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
return driver;
}

Hi could you please use following versions of Appium java client and Selenium:
Appium Java-Client : 6.0.0-BETA5
Selenium-server : 3.9.1 Appium
Desktop as Appium Inspector : 1.6.1
Also please try with the following code for driver initialization :
private static AndroidDriver<?> driver;
public static AndroidDriver getDriver() {
if (driver == null) {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "920121cb3c7fc34a");
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
caps.setCapability(MobileCapabilityType.AUTOMATION_NAME,"Appium ");
caps.setCapability(MobileCapabilityType.VERSION, "6.0.1");
caps.setCapability(MobileCapabilityType.APP, "path_of_app");
caps.setCapability("appWaitActivity", "*");
try {
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), caps);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
return driver;
}
I hope this solves your problem. Thanks!

Related

SeleniumGrid: org.openqa.selenium.WebDriverException: Unable to parse remote response: <html> <head> <title>

I'm trying to write test cases using selenium. I wrote this code. And i got this error
org.openqa.selenium.WebDriverException: Unable to parse remote response:
but I don't understand what is wrong
public static WebDriver driver=null;
ATUTestRecorder recorder;
#Parameters({"browsername"})
#BeforeTest
public void setUpBrowser(String browsername) throws ATUTestRecorderException, MalformedURLException
{
String timeStamp = new SimpleDateFormat("yy-MM-dd_HH-mm-ss").format(new Date());
recorder = new ATUTestRecorder("./ScreenRecorder/","Test_"+timeStamp+".mp4",false);
DesiredCapabilities cap =null;
if(driver==null)
{
if(browsername.equalsIgnoreCase("Firefox"))
{
// Set the path for geckodriver.exe
//System.setProperty("webdriver.gecko.driver", "./Drivers/geckodriver.exe");
//WebDriverManager.firefoxdriver().setup();
cap = DesiredCapabilities.firefox();
cap.setBrowserName("firefox");
cap.setPlatform(Platform.ANY);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);
}
// If the browser is Chrome
else if(browsername.equalsIgnoreCase("Chrome"))
{
// Set the path for chromedriver.exe
System.setProperty("webdriver.chrome.driver", "./Drivers/chromedriver.exe");
//WebDriverManager.chromedriver().setup();
cap = DesiredCapabilities.chrome();
cap.setBrowserName("chrome");
cap.setPlatform(Platform.ANY);
System.out.println(driver);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);
}
// If the browser is IE
else if(browsername.equalsIgnoreCase("IE"))
{
// Set the path for IEdriver.exe
//System.setProperty("webdriver.ie.driver", "./Drivers/IEDriverServer.exe");
//WebDriverManager.iedriver().setup();
cap = DesiredCapabilities.internetExplorer();
cap.setBrowserName("internetExplorer");
cap.setPlatform(Platform.ANY);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);
}
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(DataConfig.baseURL);
}
recorder.start();
}
How can I resolve this issue?
Remove /wd/hub from your driver URL.
See example at https://www.selenium.dev/documentation/webdriver/remote_webdriver/

appium issue, not able to launch two apps

I am not able to launch two apps in single script using APPIUM, it shows error as
An unknown server-side error occurred while processing the command.
The desired should not include both of an 'appPackage' and a
'browserName'
public class SalesLeadProg {
public AndroidDriver<MobileElement> driver;
private WebElement element; public WebDriverWait wait;
#BeforeMethod
public void setup() throws MalformedURLException, InterruptedException {
DOMConfigurator.configure("log4j.xml");
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("automationName", "UiAutomator2");
caps.setCapability("deviceName", "sunil");
caps.setCapability("udid", "422b21e7");
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "8.1.0");
caps.setCapability("appPackage", "com.mfcwl.mfc_dealer");
caps.setCapability("appActivity", "com.mfcwl.mfc_dealer.Activity.SplashActivity");
caps.setCapability("autoAcceptAlerts", true);
caps.setCapability("noReset", true);
caps.setCapability("browserName", "Chrome");
//it will launch the chrome browser in mobile
caps.setCapability(ChromeOptions.CAPABILITY, true);
ChromeOptions options=new ChromeOptions();
options.setExperimentalOption("androidPackage", "com.android.chrome");
caps.setCapability("autoGrantPermissions", true);
// to allow the permission of contacts camara, which comes in the begining when we install the app*/
caps.setCapability("unicodeKeyboard", true);
// to make the keyboard working
caps.setCapability("resetKeyboard", false);
// it
try {
driver = new AndroidDriver<MobileElement> (new URL("0.0.0.0:4723/wd/hub"),caps);
}
catch(MalformedURLException e) {
System.out.println(e);
}
driver.manage().timeouts().implicitlyWait(16, TimeUnit.SECONDS);
}
#Test
public void Dashboard() throws MalformedURLException, InterruptedException {
MobileElement email = driver.findElement(By.id("com.mfcwl.mfc_dealer:id/email"));
Thread.sleep(4000);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(email));
boolean isElementPresent = email.isDisplayed();
System.out.println(isElementPresent);
Thread.sleep(4000);
driver.findElementById("com.mfcwl.mfc_dealer:id/email").clear();
driver.findElementById("com.mfcwl.mfc_dealer:id/email").sendKeys("prestige");
System.out.println("Entered ID");
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/password")).clear();
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/password")).sendKeys("Mahindra");
System.out.println("Entered Password");
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/email_sign_in_button")).click();
//to login
System.out.println("Signed IN");
Thread.sleep(4000);
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/homeTab")).click();
//to click on home button
System.out.println("Clicked on Home button");
Thread.sleep(3000); //Set ChromeDriver location
System.setProperty("webdriver.chrome.driver", "./ChromeDriver/chromedriver.exe");
driver.get("google.com/"); driver.findElement(By.id("com.mfcwl.mfc_dealer:id/click_lead")).click();
System.out.println("Clicked on Today's follow up leads section");
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/lead_filter")).click();
Please assist with a solution on how to use two apps in APPIUM with a single script.

java.lang.NullPointerException while invoking driver.quit() within #AfterClass annotated method through Selenium WebDriver and JUnit

I created a test using JUnit and in class #AfterClass I put the driver.quit () command to shut down the browser when the tests are finished but the eclipse displays the java.lang.NullPointerException message.
The test class populates several fields and then makes a query in the base, displays the result in the Eclipse console and should close the browser but displays the java.lang.NullPointerException message.
Below is the log and test script.
public class validarStatus {
private static WebDriver driver;
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");
}
#Test
public void validarStatusOs() throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("http://10.5.9.45/BKOMais_S86825EstrategiaBackOfficeClaroFixo");
driver.manage().window().maximize();
// Logar BkoMais
driver.findElement(By.id("matricula_I")).sendKeys("844502");
driver.findElement(By.id("senha_I")).sendKeys("Pw34Jdt#*");
driver.findElement(By.id("bt_entrar")).click();
// Logar na Estratégia
driver.findElement(By.id("mn_backoffice")).click();
driver.findElement(By.id("mn_bkoffice_prod_203")).click();// Produto
driver.findElement(By.id("mn_bkoffice_est_57")).click();// Estratégia
// Selecionado a atividade
Select atividade = new Select(driver.findElement(By.id("cboAtividade")));
atividade.selectByIndex(3);
// Registro >> Novo
Thread.sleep(500);
driver.findElement(By.id("mn_registro")).click();
driver.findElement(By.id("mn_novo_caso")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// Cod Os Estratégia VREL
String CodOs = driver.findElement(By.xpath("//*[#id=\"content\"]/div[1]/fieldset[1]/div[2]/div[3]/span"))
.getText();
// Campo Análise de Contrato
Select analiseContrato = new Select(driver.findElement(By.id("cboMotivo")));
analiseContrato.selectByIndex(5);
try {
// Campo Ação
Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));
acao.selectByIndex(3);
// Status
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement ele = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("cboStatus")));
String valorStatus = ele.getText();
// driver.findElement(By.id("cboStatus")).getText();
Assert.assertEquals(" R", valorStatus);
// Chamado
driver.findElement(By.id("txtChamado")).sendKeys("Teste");
// Observação
driver.findElement(By.id("txtObservacao")).sendKeys("Teste 07/06/2018");
// Botão Salvar
driver.findElement(By.id("btnSalvar")).click();
} catch (StaleElementReferenceException e) {
// Campo Ação
Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));
acao.selectByIndex(3);
// Status
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement ele = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("cboStatus")));
String valorStatus = ele.getText();
// String valorStatus = driver.findElement(By.id("cboStatus")).getText();
Assert.assertEquals(" R", valorStatus);
// Chamado
driver.findElement(By.id("txtChamado")).sendKeys("Teste");
// Observação
driver.findElement(By.id("txtObservacao")).sendKeys("Teste 07/06/2018");
// Botão Salvar
driver.findElement(By.id("btnSalvar")).click();
} catch (Exception e) {
// Campo Ação
Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));
acao.selectByIndex(3);
// Status
String valorStatus = driver.findElement(By.id("cboStatus")).getText();
Assert.assertEquals(" R", valorStatus);
// Chamado
driver.findElement(By.id("txtChamado")).sendKeys("Teste");
// Observação
driver.findElement(By.id("txtObservacao")).sendKeys("Teste 07/06/2018");
// Botão Salvar
driver.findElement(By.id("btnSalvar")).click();
}
// Select na base para validar o status da NU_OS
ValidarEstrategiaPage p = new ValidarEstrategiaPage();
p.returnNuOs(CodOs);
// Saindo do Bko+
Thread.sleep(1000);
driver.findElement(By.linkText("Sair")).click();
}
#AfterClass
public static void closeBrowser() {
driver.quit();
}}
You clearly defined your WebDriver object as local variable inside the method:
#Test
public void validarStatusOs() throws InterruptedException {
WebDriver driver = new ChromeDriver();
In order for both the 'After' and 'Test' methods to interact with the global variable, change to:
#Test
public void validarStatusOs() throws InterruptedException {
driver = new ChromeDriver();
And BTW, change your class name from 'validarStatus' to 'ValidarStatus'. Starting a class name in upper case letter is a major best practice in Java.
As per your code block you have defined a global instance of WebDriver as:
private static WebDriver driver;
Within validarStatusOs() method you have initialized another local instance of WebDriver as:
WebDriver driver = new ChromeDriver();
When the control of your program comes out of validarStatusOs() method, the local instance of WebDriver is no more accessible.
So, when the control of your program enters within closeBrowser() method it tries to use the global instance of WebDriver and throws java.lang.NullPointerException.
Solution
As you have declared a global instance of WebDriver, use the same instance throughout your program. So you need to change the line:
WebDriver driver = new ChromeDriver();
To:
driver = new ChromeDriver();

With Selenium 3.8.1, Firefox, Chrome, and IE driver capabilities are deprecated.

Can anybody advise how to remove warnings from FirefoxDriver (capabilities) and
ChromeDriver (capabilities)?
FIREFOX
{
#Override
public DesiredCapabilities getDesiredCapabilities ()
{
DesiredCapabilities capabilities = DesiredCapabilities.firefox ();
return capabilities;
}
#Override
public WebDriver getWebDriverObject (DesiredCapabilities capabilities)
{
return new FirefoxDriver (capabilities);
}
},
CHROME_Original
{
#Override
public DesiredCapabilities getDesiredCapabilities ()
{
DesiredCapabilities capabilities = DesiredCapabilities.chrome ();
capabilities.setCapability ("chrome.switches", Arrays.asList ("--no-default-browser-check"));
HashMap<String, String> chromePreferences = new HashMap<String, String> ();
chromePreferences.put ("profile.password_manager_enabled", "false");
capabilities.setCapability ("chrome.prefs", chromePreferences);
return capabilities;
}
#Override
public WebDriver getWebDriverObject (DesiredCapabilities capabilities)
{
return new ChromeDriver (capabilities);
}
},
Try to use the following code:
ChromeOptions options = new ChromeOptions();
options.setCapability("chrome.switches",Arrays.asList("--no-default-browser-check"));
HashMap<String, Boolean>chromePreferences = new HashMap<>();
chromePreferences.put("profile.password_manager_enabled", false);
options.setCapability("chrome.prefs", chromePreferences);
ChromeDriver driver = new ChromeDriver(options);
The ChromeDriver constructor now takes in ChromeOptions object as a parameter
Manipulating DesiredCapabilities directly has been deprecated in favor of type-safe “Options” classes (FirefoxOptions InternetExplorerOptions, etc). This has the advantage of helping you avoid setting incorrect or invalid values for the driver. You get rid of the deprecation warnings by changing your code to use the newer, safer construct.

UnReachableBrowserException in Chome with Selenium Webdriver

i am getting UnhandledBrowserException when trying the below code in chrome:
public class myClass {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver", "C://Program Files (x86)//Google//Chrome//Application//chrome.exe");
WebDriver driver= new ChromeDriver();
String baseURL = "http://newtours.demoaut.com";
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = "";
// launch Firefox and direct it to the Base URL
driver.get(baseURL);
// get the actual value of the title
actualTitle = driver.getTitle();
/*
* compare the actual title of the page witht the expected one and print
* the result as "Passed" or "Failed"
*/
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
//close Firefox
driver.close();
// exit the program explicitly
System.exit(0);
}
It is launching a new session in chrome but then throwing the exception. Any help would be highly appreciated.
I think you are hitting program executable instead of driver.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
It should be path to chrome driver not the chrome browser executable.
You can take a look at https://sites.google.com/a/chromium.org/chromedriver/getting-started