java.lang.AssertionError: java.lang.NullPointerException when trying to use getdata - selenium-webdriver

I'm getting java.lang.AssertionError: java.lang.NullPointerException
Below is my code. trying to pass data from excel to a textbox. Please help (I'm calling the below method from another test)
public class myclass extends class1
{
public WebElement single = driver.findElement(By.xpath("//input[#id='IDSingle']"));
public myclass(ScriptHelper scriptHelper)
{
super(scriptHelper);
if(!driver.getTitle().contains("My Page"))
{
throw new IllegalStateException("My page expected, but not displayed!");
}
}
public void mytest()
{
driver.manage().timeouts().implicitlyWait(11,TimeUnit.SECONDS);
String sng=dataTable.getData("RMSeatPage Data", "Single");
System.out.println(sng);
single.sendKeys(sng);
}
}

Related

Getting null pointer exception while open the url in selenium automation [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
Here, I am using page object model and I want to pass the driver to other classes.But i am getting null pointer exception while launch the website (driver.get("")).
This is my base class
public class BaseClass {
public WebDriver driver;
public Logger logger = Logger.getLogger(Common.class.getPackage().getName());
public void startBrowser() {
if(driver == null) {
System.setProperty("webdriver.chrome.driver", "desktop/chromedriver.exe");
driver = new ChromeDriver();
}
}
public void quitBrowser() {
driver.quit();
}
}
and then this is my runner class:
public class TestRunnerTestNG extends AbstractTestNGCucumberTests {
BaseClass a;
#BeforeClass
public void launch()
{
a = new BaseClass();
a.startBrowser();
}
#AfterClass
public void tearBrowser()
{
a.quitBrowser();
}
}
Here, I am starting the browser using Beforeclass annotation and quit the browser using afterClass annotation.
and the following class is my page Object class: and here I have the method for launch the url:
public class SignIn extends BaseClass {
public SignIn(WebDriver driver) {
this.driver = driver ;
PageFactory.initElements(driver, this);
}
//Locators
#FindBy(id = "email")
private WebElement user_Email;
#FindBy(id = "password")
private WebElement user_Password;
#FindBy(xpath = "//span[text()='Sign In']")
private WebElement signIn_Btn;
public void landing()
{
driver.get("https://***************"); <<<< Here I am getting the null pointer exception.
}
public void signInPageGUI()
{
boolean checkSignInTextGUI = waitElement(signInText);
Assert.assertTrue(checkSignInTextGUI);
boolean CheckEmailField = waitElement(user_Email);
Assert.assertTrue(CheckEmailField);
boolean checkPwdField = waitElement(user_Password);
Assert.assertTrue(checkPwdField);
}
private void emailField(String emailName) {
user_Email.sendKeys(emailName);
}
private void passwordField(String password) {
user_Password.sendKeys(password);
}
}
and the final code is my step definition class and this is place I am calling the code.
public class LoginPage {
WebDriver driver ;
#Given("user landed to the yoco URL {string}")
public void landedOnYoCo(String string) {
System.out.println("print the string" +string);
System.out.println("driver value is " );
SignIn logIN = new SignIn(driver);
logIN.landing();
}
}
Here, Only I am calling the landing method to launch the website.
and The error is:
java.lang.NullPointerException
at pageObject.SignIn.landing(SignIn.java:83)
at stepDefs.LoginPage.landedOnYoCo(LoginPage.java:32)
at ✽.user landed to the yoco URL "https://my.yocoboard.com"(file:///Users/vinoth/Git/YoCoAutomation/src/test/resources/logIN.feature:7)
Call startBrowser before calling landing to initialize driver.
public void landedOnYoCo(String string) {
System.out.println("print the string" +string);
System.out.println("driver value is " );
SignIn logIN = new SignIn(driver);
logIN.startBrowser();
logIN.landing();
}

Wanted to embed Netflix Hystrix into an existing SMSC project

I'm trying to use Netflix Hystrix open source API in an existing project to handle fault tolerance and Circuit Breaker implementation. I have referred some websites, but I couldn't get complete information about the implementation.
How can i invoke the below Hystrix program from my service class and what object it returns i'm unable to understand.
Below is the sample program about Hystrix.
Can anybody please help me to do this.
public class CommandHelloWorld extends HystrixCommand<String> {
private final String name;
public CommandHelloWorld(String name) {
super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
this.name = name;
}
#Override
protected String run() {
return "Hello " + name + "!";
}
public static class UnitTest {
#Test
public void testSynchronous() {
assertEquals("Hello World!", new CommandHelloWorld("World").execute());
assertEquals("Hello Bob!", new CommandHelloWorld("Bob").execute());
}
#Test
public void testAsynchronous1() throws Exception {
assertEquals("Hello World!", new CommandHelloWorld("World").queue().get());
assertEquals("Hello Bob!", new CommandHelloWorld("Bob").queue().get());
}
#Test
public void testAsynchronous2() throws Exception {
Future<String> fWorld = new CommandHelloWorld("World").queue();
Future<String> fBob = new CommandHelloWorld("Bob").queue();
assertEquals("Hello World!", fWorld.get());
assertEquals("Hello Bob!", fBob.get());
}
}
}

Camel blueprint testing and cucumber

Is it possible to combine cucumber with CamelBlueprintTestSupport? I have my runner class:
#RunWith(Cucumber.class)
#CucumberOptions(monochrome=true,
format={ "pretty", "html:target/cucumber"},
features = "C:/Users/Developer/workspace_camel/SRV002_PatronInformation/src/test/resources/cucumber/asynchronousErrorHandling.feature")
public class RunFeature_SRV002_PatronInformationTest {
}
and my blueprint test class with the scenarios:
public class SRV002_PatronInformationScenarioTest extends CamelBlueprintTestSupport {
#Override
protected String getBlueprintDescriptor() {
return "/OSGI-INF/blueprint/blueprint.xml";
}
#Given("^client communicates asynchronous via socket$")
public void client_communicates_asynchronous_via_socket() throws Throwable {
System.out.println("test");
}
#When("^client posts message$")
public void an_error_occurs_inside_the_integration() throws Throwable {
String endpoint = "netty4:tcp://localhost:5000?sync=false&textline=true";
template.sendBody(endpoint, "test");
}
#Then("^the integration should not return response to the client$")
public void the_integration_should_not_return_the_error_to_the_client() throws Throwable {
System.out.println("test");
}
}
The problem now is that, when I run this I run into nullpointerexception at template.sendbody because the context, bundle and routes haven't started. For some reason it seems adding #RunWith(Cucumber) prevents the camel routes from starting.
Anyone knows how this can be solved? Thanks
Souciance
Ok so I managed to solve this.
For reference look here:
http://camel.465427.n5.nabble.com/How-to-test-routes-when-using-another-TestRunner-td5772687.html
Thanks to Gregor Lenz for the help.
Essentially the key here is that in your Camel BlueprintTestSupport class, inside the test method, that starts the given scenario you need to add this.setUp(). See the code below:
In Cucumber
SRVXXX_FileTransferCamelRunner filetransfer = new SRVXXX_FileTransferCamelRunner();
#Given("^an input file$")
public void an_input_file() throws Throwable {
endpoint.append("file:C:/Camel/input?fileName=input.txt");
}
#When("^client puts the file in the input directory$")
public void client_puts_the_file_in_the_input_directory() throws Throwable {
filetransfer.testPutFile(fileData.toString(), endpoint.toString());
}
#Then("^the integration should move the file to the output directory$")
public void the_integration_should_move_the_file_to_the_output_directory() throws Throwable {
String outputPath = "C:/Camel/output/input.txt";
filetransfer.testFileHasMoved(outputPath);
}
In Camel
#Test
public void testPutFile(String body, String endpoint) throws Exception {
this.setUp();
template.sendBody(endpoint,body);
Thread.sleep(2000);
assertFileNotExists(endpoint);
}

Selenium Webdriver_Java_How to Call Mutiple Class into One Class

I stuck at point i have one website for example (http://newtours.demoaut.com/) and for that i have created multiple classes likes:
App_Login(),
App_Dashboard(),
App_FliReservation(),
Cancel_Flight();
So want to call above all classes into single class .
can someone guide me on these.
Script sample
App_Login() class
public class App_login {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver= new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
}
}
and i want to call above class into following class
public class Mercurywebapp {
#Test
public void supp_onboarding() throws Exception {
App_Login();
}
public void App_Login() {
// TODO Auto-generated method stub
}
}
when i run above class (i.e.Mercurywebapp) then getting blank screen
Create another instances of all the Classes in the MercuryWebapp
Depends on the access modifiers for the classes
e.g
App_Login testObject = new App_Login();

Access to Solr parameters from a ValueSource Parser

So I have this class:
public class CustomValueSourceParser extends ValueSourceParser {
#Override
public ValueSource parse(FunctionQParser fqp) throws ParseException {
...
List<ValueSource> valSources = fqp.parseValueSourceList();
String iComeFromTheSolrFunctionArguments =
((LiteralValueSource)valSources.get(0)).getValue();
String iComeFromTheSolrQuery;
return new CustomValueSource(iComeFromTheSolrQuery, iComeFromTheSolrFunctionArguments);
}
}
I'd like to take the variable iComeFromTheSolrQuery from the solr query itself--not from the function arguments (because I will be calling the function multiple times and this string is very large).
Is there a way to do this? I tried adding a field to the search criteria, and then calling fqp.getParams(FIELD_NAME), but nothing came through.
Ideas?
Figured it out. What I wanted was to add a paramater. Not a field. When Formulating the query, I did this:
ModifiableSolrParams params = new ModifiableSolrParams();
params.set(PARAM_NAME_CONSTANT, paramValueString);
solrQuery.add(params);
Then in the above code I got the parameter like this:
public class CustomValueSourceParser extends ValueSourceParser {
#Override
public ValueSource parse(FunctionQParser fqp) throws ParseException {
...
List<ValueSource> valSources = fqp.parseValueSourceList();
String iComeFromTheSolrFunctionArguments =
((LiteralValueSource)valSources.get(0)).getValue();
String iComeFromTheSolrQuery=fqp.getParam(PARAM_NAME_CONSTANT);
return new CustomValueSource(iComeFromTheSolrQuery, iComeFromTheSolrFunctionArguments);
}
}

Resources