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

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();
}
}

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 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");
}
}

Inheritance in selenium webdriver using testng

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

How to execute a page object programme in eclipse?

I am basically trying to run a sample page object framework in java in Selenium .I have tried to run some sample classes given by some of the sites and forums. But for some reason, it doesnt seem to work. I dont know if I am missing out anything. Please help. Thank You
I have tried these examples -
https://weblogs.java.net/blog/johnsmart/archive/2010/08/09/selenium-2web-driver-land-where-page-objects-are-king
http://www.wakaleo.com/blog/selenium-2-webdriver-quick-tips-page-object-navigation-strategies
package google;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
public class WhenAUserSearchesOnGoogle {
private GoogleSearchPage page;
#Before
public void openTheBrowser() {
page = PageFactory.initElements(new ChromeDriver(), GoogleSearchPage.class);
page.open("http://google.co.nz/");
}
#After
public void closeTheBrowser() {
page.close();
}
#Test
public void whenTheUserSearchesForCatsTheResultPageTitleShouldContainCats() {
page.searchFor("cats");
//assertThat(page.getTitle(), containsString("cats") );
}
}
Above is the page factory class that I am using.
Following is the Page object.
package google;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.firefox.FirefoxDriver;
public class GoogleSearchPage {
protected WebDriver driver;
private WebElement q;
private WebElement btnG;
public GoogleSearchPage(WebDriver driver) {
this.driver = driver;
}
public void open(String url) {
driver.get(url);
}
public void close() {
driver.quit();
}
public String getTitle() {
return driver.getTitle();
}
public void searchFor(String searchTerm) {
q.sendKeys(searchTerm);
btnG.click();
}
public void typeSearchTerm(String searchTerm) {
q.sendKeys(searchTerm);
}
public void clickOnSearch() {
btnG.click();
}
}
The stack trace says
FAILED: whenTheUserSearchesForCatsTheResultPageTitleShouldContainCats
Your WebElement's aren't being bound by any selectors, ergo the PageFactory is failing. (it doesn't know how to find these)
Add the #FindBy annotation before each web element. e.g
#FindBy(css = "[name='q']") public WebElement q;
#Findby(css = "[name='btnG]") public WebElement btnG;
You'll get red underlines underneath #Findby. Just do a Ctrl+Shift+O to import it in.
Install the TestNG test framework to generate the report automatically

Categories