My Feature file -
Feature: Open google page
#Sanity
Scenario: Open google search
Given User open google search
Then enter text "hi" in search bar
And close the browser
###########################################################################
Page Object class -
package pageObjects;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class GoogleObjects {
public WebDriver ldriver;
public GoogleObjects(WebDriver rdriver) {
ldriver = rdriver;
PageFactory.initElements(rdriver, this);
}
#FindBy(name = "q")
#CacheLookup
WebElement search;
public void Search(String searchTxt) {
search.sendKeys(searchTxt);
search.sendKeys(Keys.DOWN, Keys.ENTER);
}
public void btnClick() {
ldriver.close();
}
}
############################################################################
StepDifinition class -
package stepDefinitions;
import org.openqa.selenium.WebDriver;
import io.cucumber.java.en.*;
import pageObjects.GoogleObjects;
public class GoogleSearch {
public WebDriver driver;
#Given("^User open google search$")
public void user_open_google_search() throws Throwable {
driver.get("https://www.google.com");
}
#Then("^enter text \"([^\"]*)\" in search bar$")
public void enter_text_something_in_search_bar(String TxT) throws Throwable {
GoogleObjects gb = new GoogleObjects(driver);
gb.Search(TxT);
}
#And("^close the browser$")
public void close_the_browser() throws Throwable {
GoogleObjects gb = new GoogleObjects(driver);
gb.close();
}
}
##########################################################################
Test Runner class -
package runner;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
#RunWith(Cucumber.class)
#CucumberOptions(
features = {".//Features/google.feature"},
glue = {"stepDefinitions"},
monochrome=true,
tags = "#Sanity",
plugin = {"pretty","html:target/reports.html" ,
"json:target/reports.json"}
)
public class testRunner {
}
######################################################################
hooks class -
package stepDefinitions;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import io.cucumber.java.After;
import io.cucumber.java.Before;
public class hooks {
WebDriver driver;
#Before
public WebDriver setUp() throws IOException {
System.setProperty("webdriver.chrome.driver","*:\\****\\***\\Drivers\\chromedriver.exe");
driver = new ChromeDriver();
return driver;
}
#After
public void tearDown() {
driver.quit();
}
}
###########################################################
I created a Maven project with cucumber framework and wrote a feature file, step
definitions, test runner and hooks file.
When I run my test runner I am getting "io.cucumber.java.InvalidMethodSignatureException", when I import io.cucumber.java.before/after but my test is running when I import from junit, but #before and #after doesn't work in cucumber if we import from juint. I not understanding why I am getting InvalidMethodSignatureException.
I tried providing order=0 for #Before annotation. It doesn't work
Above is the code, please help me.
You get this issue because methods annotated with io.cucumber.java.Before have to be void.
In your example it returns WebDriver. This is why you get InvalidMethodSignatureException.
Related
Have two classes one Login and the other which is the TestNG class, I created variable to get the Title of the page but when trying to run the test it is displaying null pointer exception, if I remove the getTitle variable it runs.
package main;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class Login {
WebDriver driver ;
By userNameTextBox = By.name("email");
By passwordTestBox = By.id("password");
By loginButton = By.id("signInButton");
String titleText = driver.getTitle();
public Login(WebDriver driver) {
this.driver=driver;
}
public void setUserName() {
driver.findElement(userNameTextBox).sendKeys("v-tobias.rivera#shutterfly.com");
}
public void setPassword() {
driver.findElement(passwordTestBox).sendKeys("Indecomm1");
}
public void clickLogin() {
driver.findElement(loginButton).click();;
}
}
package test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
//import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
//import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import main.Login;
public class LoginTest {
WebDriver driver;
Login objLogin;
#BeforeMethod
public void beforeTest() {
System.setProperty("chromedriver", "/Users/tobiasriveramonge/eclipse-
workspaceAutomation/seleniumAutomation");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://accounts.shutterfly.com/?
redirectUri=https%3A%2F%2Fwww.shutterfly.com%2F&cid=&brand=SFLY&theme=SFLY");
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(30));
WebElement element =
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("email")));
element.isDisplayed();
}
#Test
public void Test() {
objLogin = new Login(driver);
//String title = objLogin.getTitle();
// System.out.println(title);
//Assert.assertTrue(loginPageTitle.toLowerCase().contains(" "));
objLogin.setUserName();
objLogin.setPassword();
objLogin.clickLogin();
}
#AfterTest
public void afterTest() {
driver.quit();
}
}
I would like to know why am I getting this exception.
Hi, Have two classes one Login and the other which is the TestNG class, I created variable to get the Title of the page but when trying to run the test it is displaying null pointer exception, if I remove the getTitle variable it runs.
You did not initialize the driver object.
That's why referencing to that not initialized object throws the null pointer exception.
You only declared the driver object type by
WebDriver driver;
but never initialized it.
I'm using Selenium and TestNG for the first time and I've been trying to search an element by its ID but I keep getting an "Cannot instantiate class" error. This is my code:
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
public class NewTesting {
WebDriver driver = new FirefoxDriver();
#BeforeTest
public void setUp() {
driver.get("http://book.theautomatedtester.co.uk/chapter1");
}
#AfterTest
public void tearDown() {
driver.quit();
}
#Test
public void testExample() {
WebElement element = driver.findElement(By.id("verifybutton"));
}
}
Maybe I missed installing something? I installed the TestNG plug-in for eclipse and added the WebDriver JAR files, do I need to do more?
I tried following multiple tutorials but I keep getting errors, I hope someone can help. Thanks in advance!
EDIT:
I now have this:
public class NewTest {
private WebDriver driver;
#BeforeTest
public void setUp() {
System.setProperty("webdriver.gecko.driver","C:\\Program Files\\Selenium\\FirefoxDriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://book.theautomatedtester.co.uk/chapter1");
}
#AfterTest
public void tearDown() {
driver.quit();
}
#Test
public void testExample() {
WebElement element = driver.findElement(By.id("verifybutton"));
}
}
It does open the website now but I'm getting a nullpointer exception now:
FAILED CONFIGURATION: #AfterTest tearDown
java.lang.NullPointerException
at NewTest.tearDown(NewTest.java:21)
Replace this set of imports:
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
With:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
Additionally, you have to download the required format of GeckoDriver executable from mozilla/geckodriver, extract the binary and then initialize the FirefoxDriver.
Your effective code block will be:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
public class NewTesting {
WebDriver driver;
#BeforeTest
public void setUp() {
System.setProperty("webdriver.gecko.driver","C:\\path\\to\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://book.theautomatedtester.co.uk/chapter1");
}
#AfterTest
public void tearDown() {
driver.quit();
}
#Test
public void testExample() {
WebElement element = driver.findElement(By.id("verifybutton"));
}
}
If you're on windows, this previous question may be some help to you.
It mentions that you can download geckodriver, and then initialize your FirefoxDriver like this:
System.setProperty("webdriver.gecko.driver","G:\\Selenium\\Firefox driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
For the Junit Test case, I am trying to open a browser, navigate to my site and the enter an email in an field. Although all my commands are correct, I cant understand why does it specifically stops and shows error for line 33 i.e. driver.findElement(By.cssSelector)
package JUnitTesting;
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class BasicActions {
WebDriver driver;
String BaseUrl;
#Before
public void setUp() throws Exception {
//System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
driver = new FirefoxDriver();
BaseUrl = "https://www.flock.co/in/indexd/";
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#Test
public void test() {
driver.get(BaseUrl);
System.out.println("opening the base url");
driver.findElement(By.xpath("//div[#id='main-area']//input[#type='email']")).clear();
driver.findElement(By.cssSelector("._g-s-input>input")).sendKeys("testing#mailinator.com");
System.out.println("Entering a valid email id");
driver.findElement(By.xpath("//div[#id='main-area']/div[2]/div[2]//button[#class ='_g-s-button']")).click();
System.out.println("Redirecting to web.flock.co");
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}
Appropriate syntax to find element by css class is :
driver.findElement(By.cssSelector("input._g-s-input"));
I am assuming '_g-s-input' is your css class name, if not so, please replace it with appropriate css class name.
selenium code is:
package junitJmeter;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LoadTests {
WebDriver driver;
#Before
public void SetUp() {
driver = new FirefoxDriver();
}
#Test
public void testCase01LoadingFirstPage() throws Exception {
driver.get("https://www.google.com");
assertEquals("Google", driver.getTitle());
}
#After
public void tearDown() {
driver.quit();
}
}
and I have exported as jar file, put it into jmeter lib/junit folder.
in jmeter i have created
-threadGroup
-junitRequest
-viewRrsultTree
but the test not run at all, even cannot open firefox
Probably it's due to typo, Java method names should start with lowercase letter so change
public void SetUp() {
to
public void setUp() {
and it should do the trick for you.
By the way, there is a WebDriver Sampler available via JMeter Plugins, perhaps it could make your life easier.
I'm very new to OOP and Java. I came across this excellent video of page object factory on page object pattern:https://www.youtube.com/watch?v=u8XH46u1QAw. I tried to implement a similar code for a basic yahoo site and I get a Null pointer error. Can you please help me in troubleshooting this? I appreciate your time. My code is using testNG
Error from console:
FAILED: execute
java.lang.NullPointerException
at PageObject.SearchPage.updateSearch(SearchPage.java:16)
Code:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import PageObject.SearchPage;
public class PgMain {
WebDriver Browser;
#BeforeTest
public void start(){
Browser=new InternetExplorerDriver ();
}
#Test
public void execute(){
SearchPage s=new SearchPage(Browser);
s.navigateTo();
s.updateSearch();//This is when I get the NPE
}
#AfterTest
public void stop(){
}
}
package PageObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
public abstract class Abs {
WebDriver Browser;
public Abs(WebDriver Browser){
this.Browser=Browser;
}
public SearchPage navigateTo(){
Browser.get("http://finance.yahoo.com/");
return PageFactory.initElements(Browser, SearchPage.class);
}
}
package PageObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class SearchPage extends Abs {
#FindBy (id="UHSearchProperty")
private WebElement searchFinancebtn;
#FindBy (id="UHSearchBox")
private WebElement usersearchBox;
public SearchPage(WebDriver Browser){
super(Browser);
}
public SearchPage updateSearch(){
usersearchBox.sendKeys("GOOG");//NPE
searchFinancebtn.click();
return PageFactory.initElements(Browser, SearchPage.class);
}
}
you initElements only on the returned instance of SearchPage during navigateTo() call.
so for your code to work you'd have to do the following:
SearchPage s = new SearchPage(Browser);
s.navigateTo().updateSearch();
but to be honest this looks very ugly. Why not just to do the following:
WebDriver driver;
SearchPage searchPage;
#Before
public void start() {
driver = new InternetExplorerDriver();
searchPage = PageFactory.initElements(driver, SearchPage.class);
}
#Test
public void execute() {
s.navigateTo();
s.updateSearch();
}
Also I'd question the excellence of that video.