I am trying to execute the following program but getting a null pointer error. where #test is not able to access the object - driver. what could be the possible error i am making. Error -
JavaScript warning: https://www.google.com/xjs/_/js/k=xjs.s.en_US.MHBUsB8Me90.O/m=sx,c,sb,cdos,cr,elog,hsm,jsa,r,qsm,d,csi/am=wCJGjhccAPk_IRQStxAWyAImDiA/rt=j/d=1/t=zcms/rs=ACT90oGwMPBWhsFBKoM1svJAFUVoVQRQug, line 7: mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create
https://www.google.com/?gws_rd=ssl
FAILED: websiteTitle
java.lang.NullPointerException
at myPackage.TestNGforHDFC.websiteTitle(TestNGforHDFC.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
package myPackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestNGforHDFC {
public WebDriver driver;
public String urlUnderTest = "http://www.google.com";
public String projLocation = "C:\\Users\\Nikita Agrawal\\Selenium\\geckodriver.exe";
#BeforeTest
public void login()
{
System.setProperty("webdriver.gecko.driver", projLocation);
WebDriver driver = new FirefoxDriver();
driver.get(urlUnderTest);
System.out.println(driver.getCurrentUrl());
}
#Test
public void websiteTitle()
{
System.out.println(driver.getTitle());
}
}
The class variable driver is never initialized, you're defining a new variable called driver inside the login method.
Use:
public void login(){
driver = new FirefoxDriver();
....
}
Instead of:
public void login(){
WebDriver driver = new FirefoxDriver();
....
}
Related
Everything was working until two days ago I got this error:
> Task :app:compileDebugJavaWithJavac FAILED
Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.5/userguide/command_line_interface.html#sec:command_line_warnings
224 actionable tasks: 195 executed, 29 up-to-date
Note: /Users/yashatreya/Desktop/realyze/node_modules/#react-native-community/async-storage/android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStorage
Module.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /Users/yashatreya/Desktop/realyze/node_modules/#react-native-community/datetimepicker/android/src/main/java/com/reactcommunity/rndatetimepicker/RNDatePickerD
ialogFragment.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: /Users/yashatreya/Desktop/realyze/node_modules/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerButtonViewManager.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /Users/yashatreya/Desktop/realyze/node_modules/react-native-reanimated/android/src/main/java/com/swmansion/reanimated/NodesManager.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /Users/yashatreya/Desktop/realyze/node_modules/react-native-reanimated/android/src/main/java/com/swmansion/reanimated/NodesManager.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: /Users/yashatreya/Desktop/realyze/node_modules/react-native-sound/android/src/main/java/com/zmxv/RNSound/RNSoundModule.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /Users/yashatreya/Desktop/realyze/node_modules/react-native-svg/android/src/main/java/com/horcrux/svg/TSpanView.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
/Users/yashatreya/Desktop/realyze/android/app/build/generated/rncli/src/main/java/debug/facebook/react/PackageList.java:90: error: getApplication() has protected access in ReactNativeHost
return this.reactNativeHost.getApplication();
^
1 error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
This is my MainApplication.java
package com.realyze;
import android.app.Application;
import android.content.Context;
import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import io.expo.appearance.RNCAppearancePackage;
import com.oblador.vectoricons.VectorIconsPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
//MultiDex
import androidx.multidex.MultiDexApplication;
//RN-PushNotification
import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;
public class MainApplication extends MultiDexApplication implements ReactApplication {
private final ReactNativeHost mReactNativeHost =
new ReactNativeHost(this) {
#Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
#Override
protected List<ReactPackage> getPackages() {
#SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
new ReactNativePushNotificationPackage();
return packages;
}
#Override
protected String getJSMainModuleName() {
return "index";
}
};
#Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
#Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
initializeFlipper(this); // Remove this line if you don't want Flipper enabled
}
/**
* Loads Flipper in React Native templates.
*
* #param context
*/
private static void initializeFlipper(Context context) {
if (BuildConfig.DEBUG) {
try {
/*
We use reflection here to pick up the class that initializes Flipper,
since Flipper library is not available in release mode
*/
Class<?> aClass = Class.forName("com.facebook.flipper.ReactNativeFlipper");
aClass.getMethod("initializeFlipper", Context.class).invoke(null, context);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
Versions:
React Native: 0.61.4
Android Studio: 3.6.1
Gradle Plugin: 3.4.2
I have tried clearing the build cache but it still gives me the same error.
I am a beginner react-native developer and I can't understand what is going wrong.
Your help would be very much appreciated
The comments inside MainApplication.java are there to help and guide you on how to add packages that do not get to auto-link; so, if react-native-push-notification does not support auto-linking, you'll have to add it to the packages list like this:
//RN-PushNotification
import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;
public class MainApplication extends MultiDexApplication implements ReactApplication {
private final ReactNativeHost mReactNativeHost =
new ReactNativeHost(this) {
#Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
#Override
protected List<ReactPackage> getPackages() {
#SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// Add a new ReactNativePushNotificationPackage to the packages
// ONLY IF ReactNativePushNotificationPackage does not support auto-linking
packages.add(new ReactNativePushNotificationPackage());
return packages;
}
...
If it does support auto-linking, then you'll have to remove/reverse the manual installation steps, clean the project and let React Native handle the native linking with the new RN >= 60 auto-linking system.
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public class oo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","D:\\Java\\Lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://google.com");
JavascriptExecutor js = (JavascriptExecutor) driver;
Object s = js.executeScript("return document.body.innerHTML;",null).toString();
System.out.println(s);
driver.close();
}
}
Above code returns nullPointerException.
Exception in thread "main" java.lang.NullPointerException at java.util.Arrays.stream(Unknown Source) at java.util.stream.Stream.of(Unknown Source) at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:484) at oo.main(oo.java:25)
When I remove optional object parameter, it goes compilation error.
Code:
JavascriptExecutor js = (JavascriptExecutor) driver;
Object s = js.executeScript("return document.body.innerHTML;").toString();
Error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method executeScript(String, Object[]) in the type JavascriptExecutor is not applicable for the arguments (String)
Using Selenium-server-standalone-3.141.59.jar
To extract and print the Page Source through JavascriptExecutor you can use the following (Java based) solution:
Syntax:
String page_source = ((JavascriptExecutor)driver).executeScript("return document.documentElement.innerHTML;").toString();
System.out.println(page_source);
Note: You need to induce a waiter for the page to load completely before extracting the Page Source.
I am trying to get and access the elements from a pop-up/alert using selenium in java. Here is the code:
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "path_to_gecko_driver");
FirefoxDriver firefoxDriver = new FirefoxDriver();
firefoxDriver.get("some_url_which_needs_webcam_and_microphone");
WebElement element = new WebDriverWait(firefoxDriver, 20).until(ExpectedConditions.presenceOfElementLocated(new By.ByClassName("button_id")));
element.click();
JavascriptExecutor js = (JavascriptExecutor) firefoxDriver;
String title = String.valueOf(js.executeScript("return document.title"));
System.out.println("Title ==== " + title);
Object rezult = js.executeScript("return document.getElementById(\'nav-bar\')");
System.out.println("Browser element ==== " + rezult); //===>null
System.out.println("FINISH");
}
}
I am trying to access the browser pop-up elements, so I can choose between the available webcams or microphones and allow or deny the access. The JS code works just fine in the browser's console, but not in Java.
Any time I try to access any element in Java by using the JavascriptExecutor the element is null.
How can I access the pop-up elements by using the JavascriptExecutor in Java? Are there any other predefined configs or any other prerequisites that I am missing? Thank you
As you are invoking click() prior to this action we should induce WebDriverWait for the WebElement identified as getElementById('nav-bar') to render properly through ExpectedConditions.elementToBeClickable and try the following :
js.executeScript("return document.getElementById('nav-bar')");
package com.MavenLearning.Login;
import static org.testng.Assert.assertEquals;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class LoginOne {
#Test
public void LoginTestOne()
{
{
System.setProperty("webdriver.gecko.driver","C:\\Webdriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.demo.guru99.com/v4/");
driver.findElement(By.name("uid")).sendKeys("mngr105709");
driver.findElement(By.name("password")).sendKeys("jajeten");
driver.findElement(By.name("btnLogin")).click();
assertEquals(driver.getTitle(), "Guru99 Bank Manager HomePage");
String A = driver.getTitle();
System.out.println(A);
String B = "Guru99 Bank Manager HomePage";
System.out.println(B);
if (A.equals(B))
System.out.println("Page Title matches");
else
System.out.println("Page Title Doesn't Match");
}
}
}
Seniors, I have written my code in Selenium and stored it in a global method. I don't know how to call the method in the other class. When I was learning Selenium with Java I was easily calling methods to different class but stuck now with TestNG. I have tried importing the method package so everything should work but no success. Thanks in advance for help.
From Global if you mean public static then you just need to call it like below:
ClassName.functionName()
If it is not static then you need to create an object of that class and then call the function like:
MyClass my = new MyClass();
my.MyFunctionName();
Updated
You need to create a xml file and specifically all classes which you need to execute.
Example of xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="example suite 1" verbose="1" >
<test name="Regression suite 1" >
<classes>
<class name="com.first.example.demoOne"/>
<class name="com.first.example.demoTwo"/>
<class name="com.second.example.demoThree"/>
</classes>
</test>
</suite>
Source:
http://www.seleniumeasy.com/testng-tutorials/testngxml-example-to-execute-with-class-names
Video tutorial:
https://www.youtube.com/watch?v=cNhnqVWD_54
Is there a way to run the same Selenium test case on many pages without specifically defining a list of pages?
Say, for example, I have a UIMap pageset defined like this:
var map = new UIMap();
map.addPageset({
name: 'pages',
description: 'all pages',
pathRegexp: '^thisistheroot/$'
});
In the pageset, I have all the elements defined for a test script that I want to test on each page in the pageset.
All of this is added to my core extensions.
Am I able to run a test case on the entire pageset? How can I do that?
I've looked into the issue a little more. Is there a way this is possible with Jenkins? https://jenkins-ci.org/
Edit:
I was trying to avoid using selenium webdriver, but if it is possible to obtain links as you would in a UIMap, that would probably point me in the right direction as well. I would try to iterate over the links with a single test case, which can easily be done in java. I'm using java for webdriver by the way.
Thanks.
The simple answer is "no" but Selenium WebDriver is one of the best choices for sure to find the links of a page and iterate over them. There is a very similar concept of your UIMapping called PageFactory where you map all the page elements in separate classes to keep the responsibility separate which makes debugging and refactoring much easier. I have used the PageFactory concept here.
Now coming back to your question, you can easily find the list of the links present in a page. In that case you just need to write the selector little carefully. You can then easily iterate over the links and come back and forth and so on.
Proof of concept on Google
BasePage
package google;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.NoSuchElementException;
/**
* Defines the generic methods/functions for PageObjects.
*/
public class BaseClass {
protected WebDriver driver;
/**
* #param _driver
* #param byKnownElement
*/
public BaseClass(WebDriver _driver, By byKnownElement) {
//assigning driver instance globally.
driver = _driver;
this.correctPageLoadedCheck(byKnownElement);
/* Instantiating all elements since this is super class
and inherited by each and every page object */
PageFactory.initElements(driver, this);
}
/**
* Verifies correct page was returned.
*
* #param by
*/
private void correctPageLoadedCheck(By by) {
try {
driver.findElement(by).isDisplayed();
} catch (NoSuchElementException ex) {
throw ex;
}
}
}
PageObject inherited BasePage
package google;
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.How;
import java.util.List;
/**
* Created by Saifur on 5/30/2015.
*/
public class GoogleLandingPage extends BaseClass {
private static final By byKnownElement = By.xpath("//a[text()='Sign in']");
/**
* #param _driver
*/
public GoogleLandingPage(WebDriver _driver) {
super(_driver, byKnownElement);
}
//This should find all the links of the page
//You need to write the selector such a way
// so that it will grab all intended links.
#FindBy(how = How.CSS,using = ".gb_e.gb_0c.gb_r.gb_Zc.gb_3c.gb_oa>div:first-child a")
public List<WebElement> ListOfLinks;
}
BaseTest
package tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
public class BaseTest {
public WebDriver driver;
String url = "https://www.google.com/";
#BeforeClass
public void SetUpTests() {
driver = new FirefoxDriver();
//Navigate to url
driver.navigate().to(url);
//Maximize the browser window
driver.manage().window().maximize();
}
#AfterClass
public void CleanUpDriver() throws Exception {
try {
driver.quit();
}catch (Exception ex){
throw ex;
}
}
}
Link Iterator test inheriting BaseTest
package tests;
import google.GoogleLandingPage;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import java.util.List;
/**
* Created by Saifur on 5/30/2015.
*/
public class LinksIteratorTests extends BaseTest {
#Test
public void IterateOverLinks(){
GoogleLandingPage google = new GoogleLandingPage(driver);
List<WebElement> elementList = google.ListOfLinks;
for (int i=0;i<elementList.size(); i++){
elementList.get(i).click();
//possibly do something else to go back to the previous page
driver.navigate().back();
}
}
}
Note: I am using TestNG to maintain the tests and please note for lazy loading page you may need to add some explicit wait if necessary
Actually it's simple to run an IDE test against 1 specific page (base url actually): java -jar selenium-server.jar -htmlSuite "*firefox" "http://baseURL.com" "mytestsuite.html" "results.html"
So what you need to do is use jenkins (or any bash/batch script) to run that command multiple times with the base url set as "http://baseURL.com/page1", "http://baseURL.com/page2", etc.
This will only get you as far as static list of pages to test against. If you want a dynamic list you'd have to also "crawl" the pages and you could do that in the similar batch/bash script to obtain the list of pages to test against.
In this case you'd best be investing beyond selenium IDE and switch to webdriver where you'll have more power to loop and flow control.