Inheritance in selenium webdriver using testng - java

package user;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
public class Login {
public WebDriver driver;
#Test
public void Signin() throws Exception {
driver.manage().window().maximize();
Thread.sleep(5000);
driver.navigate().refresh();
driver.findElement(By.xpath("//div[#id='header']/div/div/ul/li[6]/a")).click();
driver.findElement(By.id("Email")).sendKeys("test#gmail.com");
driver.findElement(By.id("password")).sendKeys("123456");
driver.findElement(By.xpath("//tr[5]/td[2]/input")).click();
Thread.sleep(5000);
}
#BeforeTest
public void BeforeTest() {
System.setProperty("webdriver.chrome.driver", "D:\\lib\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://test.com/");
}
INHERITANCE
package user;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
public class Addtocart extends Login {
public WebDriver driver;
#Test
public void Cart() throws Exception {
Thread.sleep(5000);
driver.findElement(By.xpath("//div[2]/div/div/a")).click();
driver.findElement(By.xpath("//a[contains(#href, '/Catalog/Featured')]")).click();
driver.findElement(By.id("//div[4]/div/a/img")).click();
driver.findElement(By.id("anchorAddToWishList")).click();*/
}
public static void main(String[] args) throws Exception{
Addtocart ac = new Addtocart();
ac.BeforeMethod();
ac.Signin();
ac.Cart();
}
}
When i try to call the super class method first. the sub class method is calling first. How to call super class method initially

With TestNG Try use:
#Test(priority=1) (assign the position to execute)

you never defined 'BeforeMethod' method in your super class, just a observation.
TestNG - Number 0 has the highest priority(it’ll be executed first) and the priority goes on based on the given number i.e., 0 has the highest priority than 1. 1 has the highest priority than 2 and so on.
public class TestNG_Priority_Annotations {
#Test(priority=6)
public void c_method(){
System.out.println("I'm in method C");
}
#Test(priority=9)
public void b_method(){
System.out.println("I'm in method B");
}
#Test(priority=1)
public void a_method(){
System.out.println("I'm in method A");
}
#Test(priority=0)
public void e_method(){
System.out.println("I'm in method E");
}
#Test(priority=3)
public void d_method(){
System.out.println("I'm in method D");
}
}
Output will be:
I'm in method E
I'm in method A
I'm in method D
I'm in method C
I'm in method B

Related

Selenium FindBy Amazon Search Giving Error. (java.lang.NullPointerException)

There is the Search Class where I made a method to do amazon search, and the Main Class calls the Method searchFor()
But I keep getting the error
Exception in thread "main" java.lang.NullPointerException
package Project1;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class Search {
#FindBy(id = "twotabsearchtextbox")
WebElement search_box;
public void searchFor(String content) {
search_box.sendKeys(content);
search_box.submit();
}
}
And this is the Main Class
package Project1;
public class Main {
public static void main(String[] args) {
Search s1 = new Search();
s1.searchFor("gaming laptop");
}
}
Please refer below solution:
Main class
public class Main {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\New folder\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("Your url ");
Search s1 = new Search(driver);
s1.searchFor("gaming laptop");
}
}
Search class
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class Search {
#FindBy(id = "twotabsearchtextbox")
WebElement search_box;
WebDriver driver;
public Search(WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void searchFor(String content) {
search_box.sendKeys(content);
search_box.submit();
}
}

How to get the name of the WebElement variable name in another class

I am trying to pass the webElement name to another class for Webdriver operations.I am using the pagefactory model.
I want to print the name of the webelement variable as well in another class.
The below is the code I have.
Class A:
Class A{
#FindBy(how = How.XPATH, using = "//div[text()='Example_23']")
public WebElement exampleTab;
}
Class B:
class B{
public static void Click(WebElement objName) throws Exception
{
objName.click();
System.out.println("Clicked on"+ objName);
}
}
Desired Output:
Clicked on exampleTab
Actual Output:
Clicked on com.sun.proxy.$Proxy14
You can do that using below code :
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
class A {
public WebDriver driver;
#FindBy(how = How.XPATH, using = "//div[text()='Example_23']")
public WebElement exampleTab;
public void initElements(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
}
public class B {
public static void main(String r[]) {
A a = new A();
System.setProperty("webdriver.chrome.driver",
"D:\\ECLIPSE-WORKSPACE\\playground\\src\\main\\resources\\chromedriver-2.35.exe");
WebDriver driver = new ChromeDriver();
a.initElements(driver); // instantiating class A elements
driver.navigate().to("url");
driver.manage().window().maximize();
Click(a.exampleTab);
}
public static void Click(WebElement objName) throws Exception {
objName.click();
System.out.println("Clicked on" + objName);
}
}

How to take screenshot of all pages irrespective of the fact that the test case is passing or failing?

I have written a code to take screenshot of a page in selenium webdriver.But I want to take screenshot on every page as the control progresses from one page to next page irrespective of the fact that the test case is passing or failing. As the work on 1 page gets completed i need a screenshot. Can anyone help me?
package com.training.edureka.selenium.module7;
import org.junit.Test;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import java.io.File;
import org.apache.commons.io.FileUtils;
public class screenshotOfAllCases {
WebDriver driver;
#BeforeTest
public void openBrowser() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");
driver = new ChromeDriver();
Thread.sleep(20000);
driver.get("http://newtours.demoaut.com");
}
#Test
public void getScreenshot()
{
try {
File scrFile =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new
File("e:\\homepage.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
#AfterTest
public void closeBrowser()
{
driver.close();
}
}
try following code
public class ScreenshotUtils implements IInvokedMethodListener {
#Override
public void beforeInvocation(final IInvokedMethod method, final ITestResult testResult) {
if (method.isTestMethod()) {
//Open browser
}
}
#Override
public void afterInvocation(final IInvokedMethod method, final ITestResult testResult) {
if (method.isTestMethod()) {
//Logic to your screenshot taking
//Close browser
}
}
}
I see using Junit in import org.junit.Test, try Junit Rule in base Test class:
#Rule
public TestRule testWatcher = new TestWatcher() {
#Override
public void failed(Throwable e, Description test) {
getScreenshot()
#Override
public void succeeded(Description test) {
getScreenshot()
}
};

NullPointerException when using an object to call a method that has Selenium WebDriver code [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I'm new to JAVA and Selenium and I really want to understand why my code doesn't work and the NullPointerException is thrown.
Basically, what I want to do is call a method that has WebDriver implementations from a different class to "Master Test" class that will be executed as a JUnit test.
But every time I execute my Master Test then NullPointerException is thrown.
Here's my Master Test that will be executed:
package common_methods;
import org.junit.*;
public class Master_Test extends Configurations {
#Before
public void setUp(){
try{
testConfiguration();
driver.get("http://only-testing-blog.blogspot.ro/");
} catch (Exception e){
System.out.println("setUp Exception: "+e);
}
}
#After
public void tearDown(){
driver.close();
}
#Test
public void masterTest(){
try{
TestCase1 testy1 = new TestCase1();
testy1.test1();
}catch (Exception master_e){
System.out.println("Test Exception: "+master_e);
}
}
}
Now for better understanding here is the Configurations Class that is being extended:
package common_methods;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class Configurations {
public WebDriver driver;
public void testConfiguration() throws Exception{
System.setProperty("webdriver.chrome.driver", "D:\\Browser_drivers\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
}
}
And here is the TestCase1 Class from which I get my method:
package common_methods;
import org.openqa.selenium.*;
public class TestCase1 extends Configurations{
public void test1() throws Exception{
driver.findElement(By.id("tooltip-1")).sendKeys("Test Case 1 action");
Thread.sleep(5000);
}
}
Why do I get the NullPointerException?
In Master_Test, where you are calling
TestCase1 testy1 = new TestCase1();
you need to pass the driver reference, so that it doesn't give the NullPointerException. Also, you would need to add a constructor in TestCase1 class to handle the driver.
Check the code below. I have used it with FirefoxDriver & Google -
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Configurations {
public WebDriver driver;
public void testConfiguration() {
driver = new FirefoxDriver();
}
}
Master_Test
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
public class Master_Test extends Configurations {
#Before
public void setUP() {
testConfiguration();
driver.get("http://www.google.com");
}
#Test
public void masterTest() {
TestCase1 test = new TestCase1(driver);
test.test1();
}
}
TestCase1
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class TestCase1 extends Configurations {
public TestCase1(WebDriver driver) {
this.driver = driver;
}
public void test1() {
driver.findElement(By.id("lst-ib")).sendKeys("test");
}
}

calling multiple methods within a method - selenium webdriver cross browser testing

I have a question with regards to calling multiple method using JUNIT. This is my test
package com.example.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;
public class test {
private WebDriver _driver;
#Test
public void FFconfiguration() throws Exception {
System.out.println("Running FF");
_driver = new FirefoxDriver();
_driver.get("URL");
login();
setup();
_driver.quit();
}
public void login1()
{
}
public void setup()
{
}
}
My question is: Can I call both login() and setup() within the method FFConfiguration? If not what's the alternate solution...............
Yes, absolutely, you can do it. You can have tests like this:
#Test
public void testBuyingProcess(){
ShoppingUI shoppingPage = new ShoppingUI();
shoppingPage.login();
Assert.assertEquals(shoppingPage.getTitle(),"Welcome");
//....
}
And fill in the methods elsewhere even in different class. Few examples of methods used above:
public class ShoppingUI{
private WebDriver driver
public ShoppingUI(){
driver = new FirefoxDriver();
driver.get("http://my-test-site.com/buy-buy-buy.html");
}
public String getTitle(){
return driver.getTitle();
}
}

Categories