I have created a class 'a' with in package name 'pack1'
#Beforetest
public class a {
public properties prop;
public propLoad(){
Webdriver driver= new firefoxdriver();
prop = new properties();
prop.load(driver);
}
also i have created a new package 'pack2' and new class 'b' and i want to use the prop object in b class
i have written the code as
#Test
public class b extends a{
prop.getproperties(keyname);
}
(Keys and values are defined in .properties file)
But when i have ran the code system gives me null pointer exception
Pls help me in this issue
Define prop = new properties(); at class level
#Beforetest
public class a {
public properties prop=new properties();
public propLoad(){
Webdriver driver= new firefoxdriver();
prop.load(driver);
}
I am not sure for what reason you need to pass driver object here? prop.load(driver); Anyways, instantiating prop class object at class level should do a trick. Let me know.
Related
As I went through tutorial, all written about get() method is example of method overriding.
But as per method overriding method which is present in parent class is having different implementation in child class.
And get() method is declared webdriver interface and implemented in RemoteWebDriver interface.
Then how overriding is achieved here?
Could you please help with query.
Any other example with method overriding in selenium webdriver please share?
If you are asking specifically for the get() function, here is the implementation tree:
public class ChromeDriver extends RemoteWebDriver
implements LocationContext, WebStorage, HasTouchScreen, NetworkConnection {...}
Here chromeDriver is an example class can be any other. It extends the RemoteDriver and implements a bunch of other interfaces. And the RemoteDriver Class extends and implements some more as below
public class RemoteWebDriver implements WebDriver, JavascriptExecutor,
FindsById, FindsByClassName, FindsByLinkText, FindsByName,
FindsByCssSelector, FindsByTagName, FindsByXPath,
HasInputDevices, HasCapabilities, Interactive, TakesScreenshot {
...
public void get(String url) {
execute(DriverCommand.GET, ImmutableMap.of("url", url));
...
}
As you can see the RemoteDriver is overrridng the get() method it gets from the WebDriver interface
public interface WebDriver extends SearchContext {
....
void get(String url);
....
}
This is a simple overriding example of the "findElement" method.
public static WebElement findElement(By Locator){
WebElement anElement = fluentWait.until(new Function<WebDriver, WebElement>() {
#Override
public WebElement apply(WebDriver webDriver) {
webDriver=sampleDriver;
return sampleDriver.findElement(Locator);
}
});
return anElement;
}
Overriding something we give different implementation in child class . And override method which was in parent.
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();
I have a driver object initialized in class sample.I want to pass the driver object to other classes also but i get a null pointer exception. My code is
sample class
public class sample {
WebDriver driver ;
#Test(priority=1)
public void openbrowser(){
System.setProperty("webdriver.chrome.driver",
"/home/ss4u/Desktop/Vignesh/jars/chromedriver");
driver = new ChromeDriver();
driver.get("http://www.google.com");
System.out.println(driver instanceof WebDriver);
}
#Test(priority=2)
public void maximize(){
driver.manage().window().maximize();
}
#Test(priority=3)
public void transfer_instance(){
sampleone obj=new sampleone(driver);
}
}
sampleclassone
public class sampleone {
WebDriver driver;
public sampleone(WebDriver driver){
this.driver=driver;
System.out.println(driver instanceof WebDriver);
System.out.println(this.driver instanceof WebDriver);
System.out.println("constructor2");
}
public sampleone(){
System.out.println("Default constructor called");
}
#Test(priority=1)
public void gettitle(){
System.out.println(this.driver instanceof WebDriver);
System.out.println(driver instanceof WebDriver);
String title=this.driver.getTitle();
System.out.println(this.driver instanceof WebDriver);
System.out.println(title);
Assert.assertEquals(title, "Google");
}
#Test(priority=2)
public void navigate(){
this.driver.get("https:in.yahoo.com");
}
}
Testng xml file
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestNG" verbose="1" >
<test name="sample test">
<classes>
<class name="testsample.sample" />
</classes>
</test>
<test name="sample testone">
<classes>
<class name="testsample.sampleone" />
</classes>
</test>
</suite>
This issue occurs as iam calling the class not using the object created but using the testng.xml file is there any possible way to create a new java instance(common for all classes) or use the existing instance in all classes
I found a solution myself...When i read about the testng in detail i found that testng xml file calls the default constructor of all classes specified in the xml file.so even if we pass the object to another class we cannot perform the action through the object so null pointer exception occurs....i found two solutions first one is to use a pagefactory and second one is to use a common driver class for your Test suite...so that we can use the same driver instance in all classes
Common driver class
public class Driver {
public static WebDriver driver=null;
public static WebDriver startdriver(String browser){
if(browser.equalsIgnoreCase("Chrome")){
System.setProperty("webdriver.chrome.driver", "/home/vicky/Documents/Jars/chromedriver");
driver=new ChromeDriver();
}else if(browser.equals("Firefox")){
driver=new FirefoxDriver();
}
return driver;
}
}
It is very easy to use with extends keyword same as Java. Just you need to create common webdriver class where you will open required browser and application url with the help of TestNG annotations as below code.
WebDriver Common Class:
public class Seleniumlinktext {
public WebDriver driver;
String baseurl = "http://www.google.co.in";
#BeforeTest
public void openBrowser(){
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
Another Class:
public class WebDriverTest extends Seleniumlinktext {
#Test(priority=1)
public void linkText(){
//images hyperlink
driver.findElement(By.linkText("Images")).click();
System.out.println("Click on Images hyperlink");
}
Like this way you can pass webdriver instance to all other clases. I found the solution from this site.
Here I have used #BeforeTest annotation because my application url and browser should be open only once before starting my Test cases execution with #Test annotation.
If I have a class as follows:
class MyClass
{
List<String> list{get;set;}
...
}
And then execute:
MyClass instance = new MyClass();
instance.add('string') ;
Will that new entry in the list be added to the member variable instance?
No. What you trying to do should be more like
class MyClass{
public List<String> list{get;set;}
}
and then execute
MyClass instance = new MyClass();
instance.list.add('string');
With the simplified example below:
I get the following, as expected:
{"person":{"name":"john","tags":["tag1","tag2"]}}
However, if I only set one tag, I get this:
{"person":{"name":"john","tags":"tag1"}}
And I was expecting to get this:
{"person":{"name":"john","tags":["tag1"]}}
That is, jettison has removed the array for tags, because there is only one element in the array.
I think this is pretty unsafe.
How to force jettison to write an array, even if there is only one element?
Note: I am aware that there are other alternatives to jettison, such as StAXON.
However, here I am asking how to achieve this using Jettison.
Please do not suggest another alternative to jettison.
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.*;
import java.io.*;
import javax.xml.bind.*;
import javax.xml.stream.XMLStreamWriter;
import org.codehaus.jettison.mapped.*;
public class JettisonTest {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Person.class);
Person person = new Person();
person.name = "john";
person.tags.add("tag1");
person.tags.add("tag2");
Configuration config = new Configuration();
MappedNamespaceConvention con = new MappedNamespaceConvention(config);
Writer writer = new OutputStreamWriter(System.out);
XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con, writer);
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(person, xmlStreamWriter);
}
}
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
class Person {
String name;
List<String> tags = new ArrayList<String>();
}
I found this: https://blogs.oracle.com/japod/entry/missing_brackets_at_json_one
It seems that adding a line to your context resolver to explicitly state that tags is an array is the way to do this; i.e.
props.put(JSONJAXBContext.JSON_ARRAYS, "[\\"tags\\"]");
NB: I'm not familiar with Jettison, so have no personal experience to back this up; only the info on the above blog post.
#Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;
private Class[] types = {ArrayWrapper.class};
public JAXBContextResolver() throws Exception {
Map props = new HashMap<String, Object>();
props.put(JSONJAXBContext.JSON_NOTATION, "MAPPED");
props.put(JSONJAXBContext.JSON_ROOT_UNWRAPPING, Boolean.TRUE);
props.put(JSONJAXBContext.JSON_ARRAYS, "[\\"tags\\"]"); //STATE WHICH ELEMENT IS AN ARRAY
this.context = new JSONJAXBContext(types, props);
}
public JAXBContext getContext(Class<?> objectType) {
return (types[0].equals(objectType)) ? context : null;
}
}