Appium : NullPointerException coming - selenium-webdriver

I am trying to run the below code to automate my mobile application and every thing seems OK but I am getting NullPointerException on following line:
driver.findElement(By.id("com.app.aftertax.aftertax:id/Text7"));
Here is my code:
public class Login {
public static AndroidDriver driver;
#BeforeTest
public void setUp() throws Exception {
File classpathRoot = new File(System.getProperty("user.dir"));
File app = new File("/Users/hanan/Downloads/app-at.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("platformVersion","4.4");
capabilities.setCapability("app-package", "com.mobikwik_new");
capabilities.setCapability("app-activity", ".MobikwikMain");
capabilities.setCapability("app-wait-activity",
".MobikwikMain");
capabilities.setCapability("appiumVersion", "1.3.7");
//capabilities.setCapability("name", methodName.getName());
capabilities.setCapability("app", app.getAbsolutePath());
new AndroidDriver( new URL("http://localhost:4723/wd/hub"),
capabilities);
}
#Test
public void apiDemo() throws InterruptedException {
Thread.sleep(10000);
WebElement login = driver.findElement(By.id("com.app.aftertax.aftertax:id/Text7"));
login.click();
}
}

If your code line is the first in the stack trace (you should add the full stacktrace to such questions), then it's the driver variable which is not initialized (==null)

It seems that you are not assigning Appium object in below code:
new AndroidDriver( new URL("http://localhost:4723/wd/hub"),
capabilities);
You should create reference variable of AppiumDriver or AndroidDriver at class level. then assign created object in reference variable like below:
driver = new AndroidDriver( new URL("http://localhost:4723/wd/hub"),
capabilities);

Related

Multiple screenshot using selenium webdriver

What are the steps to take multiple screenshots in selenium without updating the previous screenshot?
Use this:
class Main {
public static void main(String [] args) throws Exception {
WebDriver driver = new ChromeDriver(); driver.manage().window().maximize();
driver.get("irctc.co.in/");
Date d =new Date();
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("E:\\selenium\\screenshot\\"+d.toString().replace(":", "_")+".png"));
}
}

I'm trying to launch a chromedriver in seleniumwebdriver even i give the right path its show error

public class sampledr {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//Thread.sleep(3000);
System.setProperty(" webdriver.chrome.driver","C:\\Program Files (x86)\\Common Files\\chromedriver.exe" );
WebDriver d= new ChromeDriver();
It's show illegal state exception.
Your code was almost perfect but have a issue. When you mention any Key-Value pair through System.setProperty, these values are String and shouldn't contain any spaces within. So you need to update the line as :
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Common Files\\chromedriver.exe" );
public class sampledr {
public WebDriver driver; //it creates the driver object
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Common Files\\chromedriver.exe" ); //set driver path
wd = new ChromeDriver(); //Call driver
driver.get("https://www.google.com"); //open site

When I run a test suite with ChromeDrive Chrome is opened at every test

I'm trying to use the same code with Appium driver and Java , TestNG but with ChromeDriver I changed the configuration by adding that code :
File file = new File("C:/QA/Emna/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driver = new ChromeDriver();
The problem is that any test case the chrome window is opened a new one, even if my tests are in a correct order with Priority (by TestNG).
Is there a way to work in only one window?
WebDriver driver = new ChromeDriver(); is what opens a new browser each time. Move it to #BeforeClass section and use the same instance in all the tests.
You need to move you code snippet in any #Before type of TestNG methods. Lets say your all test cases are in a TestNG class then
Here is how it should look like:
#BeforeClass
public void deSetup(){
File file = new File("C:/QA/Emna/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driver = new ChromeDriver();
}
But if this is not the case, what I mean by this, that your test cases are spread across the multiple TestNG classes then the best way is to have a Singleton class to load and initialize the ChromeDriver. Call method of the Singleton class to instantiate the ChromeDriver in any one of method annotated as #BeforeSuite #BeforeTest or #BeforeGroups. And have a reference variable of WebDriver type in evelry test class and assigne the previously initialized ChromeDriver to this reference variable in #BeforeClass method.
In such a way, ChromeDriver will be instantiated only once when any one of #BeforeSuite #BeforeTest or #BeforeGroups method runs and will be available in every TestNG class once #BeforeClass runs.
This way you can work in only one Chrome window.
I have fixed my problem by this way in #BeforeClass:
#BeforeClass
public static void before() {
// check active session
System.out.println("Becore Test Method");
File file = new File("C:/QA/Emna/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
wd = new ChromeDriver();
}
I made an instance like that:
static WebDriver driver ;
Then in every test I put the following :
#Test
public static void run1() {
//my tests using wd
}
#Test
public static void run2() {
//my tests using wd
}

Null pointer exception due to driver instance is null in onTestFailureMethod

Need help on - I am getting null pointer exception in onTestFailure method. If any of my #Test method fails control goes to onTestFailure but driver is null in that method. my code is like -
import statements...
#Listeners(ScreenShot.class)
public class ScreenShot implements ITestListener{
WebDriver driver;
#BeforeClass
public void launch(){
System.setProperty("webdriver.ie.driver", "D:\\Jars\\Drivers\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.get("url");
}
#Test
public void test1(){
//driver.findElement(By.id("Email")).sendKeys("E#E.COM");
System.out.println("Method1 begins");
//some code here - exception occurs here
System.out.println("Method ended");
}
public void onTestFailure(ITestResult result){
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //Getting exception here as driver is null
try {
FileUtils.copyFile(scrFile, new File("C:\\snaps\\"+result.getMethod().getMethodName()+".png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
i am running this class by right click->run as testng.
The reason you are getting a NullPointerException is because Testng creates a new instance of each listener. So that new instance has driver not initialized.
In your case, the object of Screenshot class running the #Test method and the instance of the Screenshot class as a listener being invoked by TestNG is different.
There are couple of ways to solve this.
Move the code from onTestFailure to #AfterMethod with the ITestResult as the method argument. Work on the result data to take screenshot.
#AfterMethod
public void afterMet(ITestResult res){
if(res.isSuccess())
Make the driver as a global static. - if you plan to run only sequential tests
Consider threadlocal if you plan to run parallely.

Different Screen Shot Resolution in Different Browsers

I am facing a problem related to my Project of GUI comparison...
It takes Screen Shots of given URL in different Browsers, but these Screen Shots are having different Resolution for different Browser.
So, my problem is that now what to do for getting the Same Resolution of all the screen shots in different Browsers.???
If any solution is there then kindly tell me.
Detail:
Resolutions With:
Mozilla Firefox:- 1345*627
Google Chrome:- 1345*659
Internet Explorer:- 1345*679
Tools used:
Selenium Web Driver.
Java
Try maximising the driver window
Example with Junit & webdriver:
public class Untitled {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.google.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
#Test
public void testUntitled() throws Exception {
driver.get(baseUrl);
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}`

Resources