RemoteWeb Driver driver not initializing because param-value-not-found - java

I am trying to run some basic tests using Selenium Grid and I acquired the code below from my instructor.
However RemoveWebDriver driver is always NULL as device is always param-value-not-found
I am unable to understand what is setting the value of the parameter device and how to correct this error.
My knowledge of TestNG is limited. I am guessing TestNG is responsible for calling the openBrowser function which has a parameter device the value of this parameter is always param-value-not-found.
Therefore in the if else block device does not equal pc1 or pc2 which causes driver vairable to have a value Null.
I am unable to figure out how the value of device is getting derived.
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class gridTest
{
public static WebDriver driver ;
#Parameters("System")
#Test(priority=0)
public void openBrowser(String device) throws MalformedURLException // device is always param-value-not-found
{
System.out.println("device is : " + device);
if (device.equalsIgnoreCase("pc1"))
{
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability("browserVersion", "80");
driver = new RemoteWebDriver(new URL("http://192.168.1.6:30032/wd/hub"), cap);
}
else if (device.equalsIgnoreCase("pc2"))
{
DesiredCapabilities cap = DesiredCapabilities.firefox();
driver = new RemoteWebDriver(new URL("http://192.168.1.6:30032/wd/hub"), cap);
// adding the following else block merely bypasses the issue
} else {
String nodeUrl = "http://192.168.1.6:30032/wd/hub" ;
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL(nodeUrl), capabilities);
}
}
#Test(priority = 1)
public void Moda()
{
try
{
//Navigate to URL
driver.get("https://www.flipkart.com/");
driver.manage().window().maximize();
}
catch (Exception e)
{
System.out.println(e);
return;
}
// Close login modal screen
driver.findElement(By.xpath("//*[#class='_2AkmmA _29YdH8']")).click();
// Hover the menu Electronics >> MI
WebElement men1 = driver.findElement(By.xpath("//li[#class='Wbt_B2 _1YVU3_'][1]"));
Actions act = new Actions(driver);
act.moveToElement(men1).perform();
Thread.sleep(2000);
WebElement men2 = driver.findElement(By.xpath("//li[#class='_1KCOnI _3ZgIXy'][1]//a[contains(#href,'Electronics_0_Mi')]"));
men2.click();
//Verify 'Latest from MI' label on the search result page
try
{
Thread.sleep(3000);
}
catch(InterruptedException ie){
System.out.println("Error at line 76") ;
}
boolean Validate = driver.findElement(By.xpath("//p[text()='Latest from MI : ']")).isDisplayed();
System.out.println("Latest from MI element is displayed--" + Validate);
System.out.println("Completed.") ;
}
}

This means that your test expects to have System parameter value in your testng.xml file. You have to either add one or add #Optional annotation to your method parameter so that the parameter would have been set to that optional value if no values are detected in testng.xml.
See details here.

Related

Set Value method puts the value too fast

The setValue method putting value in the field too fast and losing some chars during a process.
sendKeys method doesn't work correctly too.
[https://pp.userapi.com/c849532/v849532534/1d4fe6/BEVb5_C3O8E.jpg][]
Appium Server 1.13.0
package appiumtests2;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
import static java.lang.Thread.sleep;
public class Stoloto {
/*
* static WebDriver driver;
* AndroidDriver driver;
*/
static AppiumDriver<MobileElement> driver;
public static void main(String[] args) {
try {
Stoloto stoloto = new Stoloto();
stoloto.openStoloto();
} catch (Exception exp) {
System.out.println(exp.getCause());
System.out.println(exp.getMessage());
exp.printStackTrace();
}
}
#Test
public void openStoloto() throws Exception {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "Pixel XL");
cap.setCapability("udid", "HT6B70200690");
cap.setCapability("platformName", "Android");
cap.setCapability("platformVersion", "9");
cap.setCapability("appPackage", "ru.stoloto.mobile");
cap.setCapability("appActivity", "ru.stoloto.mobile.ca.presentation.ui.activity.MainActivity");
URL url = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AppiumDriver<MobileElement>(url, cap);
System.out.println("Application Started...");
MobileElement onBoardContinue = driver.findElement(By.id("ru.stoloto.mobile:id/confirm"));
onBoardContinue.click();
onBoardContinue.click();
MobileElement onBoardLogin = driver.findElement(By.id("ru.stoloto.mobile:id/login"));
onBoardLogin.click();
MobileElement loginField = driver.findElement(By.id("ru.stoloto.mobile:id/user"));
String login = "testtesttest#gmail.com";
sleep(1000);
loginField.setValue(login);
MobileElement passwordField = driver.findElement(By.id("ru.stoloto.mobile:id/passwordInputEdit"));
String password = "qwertyqwerty";
sleep(1000);
passwordField.setValue(password);
driver.hideKeyboard();
MobileElement log_in = driver.findElement(By.id("ru.stoloto.mobile:id/buttonSubmit"));
log_in.click();
System.out.println("Test Completed");
}
}
I need to find a way to set value with a little delay or the other way, which help me to solve this problem.
You can use mobile:shell command for sending text to the device via adb shell
Map<String, Object> argv = new HashMap<>();
argv.put("command", "input");
argv.put("args", Lists.newArrayList("text", "your_text_here"));
driver.executeScript("mobile: shell", argv);
Alternatively you can consider using elementSendText function avaiable via Appium SeeTest Extension
driver.executeScript("seetest:client.elementSendText(\"NATIVE\", \"id=your_element_id\", \"0\", \"your_text_here\")");
try to use WebDriverWait in order to set the value

Unable to perform simple click operation using appium code on Android app

I am new to Appium and I was trying to execute a simple program which performs a click operation. But the click operation is not happening. Here is the code:
package com.android.touchactionss;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
public class Sample {
public static void main(String[] args) throws InterruptedException, MalformedURLException {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("platformName", "Android");
cap.setCapability("deviceName", "xiaomi-2014818-204648717d62");
cap.setCapability("version", "5.1.1");
cap.setCapability("appActivity", "com.mediamushroom.copymydata.app.EasyMigrateActivity");
cap.setCapability("appPackage", "com.mediamushroom.copymydata");
AndroidDriver<?> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
Thread.sleep(5000);
try{
System.out.println("STARTED");
driver.findElementByAndroidUIAutomator(
"new UiSelector().resourceId(\"com.mediamushroom.copymydata:id/NextButton\")");
//driver.findElement(By.id("//*[#resource-id='com.mediamushroom.copymydata:id/NextButton']"));
System.out.println("ENDED");
}
catch(Exception exception){
exception.printStackTrace();
}
Thread.sleep(5000);
driver.quit();
}
}
No exception is thrown but the click operation didn't happen. I tried with both driver.findElement(By.id("")) and driver.findElementByAndroidUIAutomator() method. But none of them worked. I have attached the object properties screen.
Versions used: appium software version 1.6.2
appium java_client version 6.1.0
selenium 3.13
First, add the following import:
import io.appium.java_client.android.AndroidElement;
Next change your code:
AndroidDriver<?> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
to:
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
You might need to change the URL to 0.0.0.0 but it depends on what your Appium Server settings are. They may be correct they way it is now.
Lastly, you need to use the following method to click the element:
driver.findElement(By.id("com.mediamushroom.copymydata:id/NextButton")).click();
Hi here is example in next few lines, try to use some testing framework, Junit, TestNg, I've removed main, used TestNG with this example:
Start Appium server:
Appium GUI (https://github.com/appium/appium-desktop/releases/tag/v1.6.2)
Appium via console : appium --address 127.0.0.1 --port 4723
when Appium server is up-an-running, call this code:
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
public class TestAppium {
AndroidDriver<MobileElement> driver;
#BeforeTest
public void setup() {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("platformName", "Android");
cap.setCapability("deviceName", "emulator-5554"); //used emulator, but should be set devices guid in Your case "xiaomi-2014818-204648717d62"
cap.setCapability("version", "5.1.1");
cap.setCapability("appActivity", "com com.mediamushroom.copymydata.app.EasyMigrateActivity");
cap.setCapability("appPackage", "com.mediamushroom.copymydata");
try {
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
#Test
public void testAppiumSimulator() {
MobileElement element = driver.findElement(By.id("NextButton"));
element.click();
// do some Assertion
Assert.assertTrue(//some condition//);
}
#AfterTest
public void tearDown() {
driver.quit();
}
}
And this is an simple Appium test...
Hope this helps,

How to initialize the driver to avoid NullPointerException error

I am having an issue with this class where I am receiving a null pointer exception. I inserted System.out.println("driver=" + driver); to see what is outputted and it states the driver=null for each dataset which makes me think there is a problem with the initialization when I hit the method under #Test. How can I resolve this initialization of the driver to get my tests to pass in testNG?
Below is the code:
package com.testng.practice;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class LoginTest {
WebDriver driver = null;
#BeforeTest
public void invokeApplication() {
System.setProperty("webdriver.chrome.driver", "xxx\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com");
}
#Test (dataProvider = "getData")
public void loginFaceBook(String email, String password) {
System.out.println("driver=" + driver);
WebElement emailField = driver.findElement(By.name("email"));
WebElement passwordField = driver.findElement(By.name("pass"));
emailField.sendKeys(email);
passwordField.sendKeys(password);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#DataProvider
public Object[][] getData(){
//declared object of 4 rows and 2 columns
Object[][] dataSet = new Object[4][2];
dataSet[0][0] = "TimSmith#gmail.com";
dataSet[0][1] = "Smith123";
dataSet[1][0] = "JaneMcCormack#gmail.com";
dataSet[1][1] = "McCormack123";
dataSet[2][0] = "AnjaliPrakash#gmail.com";
dataSet[2][1] = "Prakash123";
dataSet[3][0] = "JamesBean#gmail.com";
dataSet[3][1] = "Bean123";
return dataSet;
}
}
This definition of the driver variable is valid within the invokeApplication() method and leaves the class attribute driver uninitialized
WebDriver driver = new ChromeDriver();
to initialize the class attribute use:
this.driver = new ChromeDriver();
I think the problem should be in the #BeforeTest annotation if you are running the class it self by running as TestNG.
BeforeTest annotation is working before the tag in the xml runner file.
Here is the definition of BeforeTest from TestNG documentation.
#BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run. 
If you want to run before #Test you need to use #BeforeMethod.
Please pay attentionto the differences between all the annotation.
You can find differences here.
you declare WebDriver driver=null; Globally.
Then No Need To Declare once in Method invokeApplication();
write only
WebDriver driver = null;
#BeforeTest
public void invokeApplication() {
System.setProperty("webdriver.chrome.driver", "xxx\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com");

How to effectively change the browser name of the WebDriver object?

I intend to perform some tests by using selenium with multiple web browsers. To distinguish between the different web drivers, I use the following line of code:
((RemoteWebDriver) driver).getCapabilities().getBrowserName();
This will return a String indicating the web browser that is used by the driver object. However, for my Opera WebDriver object this will give me the String 'chrome'. I have tried changing this by explicitly setting the browser name to 'opera' using DesiredCapabilities:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("opera");
WebDriver driver = new OperaDriver(capabilities);
Unfortunately, this does not fix my problem. How do I effectively change the web browser name?
Your basic requirement is to identify browser initialization if I'm right, which can be done by getting user-agent from your browser using JavascriptExecutor as following:
String userAgent = (String) ((JavascriptExecutor) driver).executeScript("return navigator.userAgent;");
//following is for identifying opera browser initialization
if(userAgent.contains("OPR/"){
System.out.println("Browser currently in use is Opera");
}
Similarly you can identify other browser initilisation by referring this link
Unfortunately , you would not be able to change the BrowserName.
What instead you can try is to create function for specifically handling multiple browsers: -
package multiBrowser;
import org.testng.annotations.Test;
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.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.testng.annotations.Parameters;
public class MultiBrowserClass {
WebDriver driver;
#Test
#Parameters("browser")
public void multiBrowsers(String browserName) throws InterruptedException{
if(browserName.equalsIgnoreCase("firefox")){
System.setProperty("webdriver.firefox.marionette","D:\\My Work\\Setup\\JAR\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("default");
driver = new FirefoxDriver(myprofile);
}
if(browserName.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "D:\\My Work\\Setup\\JAR\\driver\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browserName.equalsIgnoreCase("IE")){
System.setProperty("webdriver.ie.driver", "D:\\My Work\\Setup\\JAR\\driver\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
else if(browserName.equalsIgnoreCase("opera")){
System.setProperty("webdriver.opera.driver", "D:\\My Work\\Setup\\JAR\\driver\\operadriver.exe");
driver = new OperaDriver();
}
driver.manage().window().maximize();
driver.navigate().to("https://");
System.out.println(driver.getTitle());
driver.findElement(By.xpath("//div[#id='navbar-main']/ul/li[5]/a")).click();
driver.findElement(By.xpath("//div[#id='navbar-main']/ul/li[5]/ul/li/a")).click();
Thread.sleep(3000);
driver.findElement(By.name("email")).clear();
driver.findElement(By.name("email")).sendKeys("abc#mm.kk");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("1qaz2wsx");
Thread.sleep(3000);
driver.findElement(By.xpath("//form[#id='loginform']/div[8]/button")).click();
Thread.sleep(5000);
if(driver.getPageSource().contains("Welcome abc#mm.kk")){
System.out.println("User Successfully logged in");
}else{
System.out.println("Username or password you entered is incorrect");
}
driver.quit();
}
}
=======

Cannot instantiate the type FirefoxDriver

Can seem to make this code execute. Compiler isn't instantiating the driver. what can I do to correct this.
package mypackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
org.openqa.selenium.support.ui.ExpectedConditions;
import
org.openqa.selenium.support.ui.WebDriverWait;
public class selenium {
public static void main(String[] args) {
WebDriver driver = (WebDriver) new FirefoxDriver();
WebDriverWait MyWaitlVar= new
WebDriverWait(driver, 10);
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);
}
}

Categories

Resources