My cucumber runner class cannot seem to find my step definition class, but it finds the feature file just fine.
Here is a snapshot of my very simple project structure:
Feature file:
Feature: Customer must be able to reach the Home Delivery page by clicking the Home Delivery button
Scenario: Test Home Delivery button
Given PS Restaurants web page is open
When User clicks on Home Delivery button
Then Home Delivery page should load
Step Definition class:
package stepDefinitions;
import java.util.concurrent.TimeUnit;
public class E_1_US_1 {
#Given("^PS Restaurants web page is open$")
public void ps_Restaurants_web_page_is_open() throws Exception {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#When("^User clicks on Home Delivery button$")
public void user_clicks_on_Home_Delivery_button() throws Exception {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#Then("^Home Delivery page should load$")
public void home_Delivery_page_should_load() throws Exception {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
}
Runner class:
package runner;
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
import cucumber.api.CucumberOptions;
#RunWith(Cucumber.class)
#CucumberOptions(
features= {"src/features/Feature_1_1.feature"},
glue= {"src/stepDefinitions/E_1_US_1.java"}
)
public class Runner {
}
The output from running the Runner class as a JUnit test:
1 Scenarios ([33m1 undefined[0m)
3 Steps ([33m3 undefined[0m)
0m0.040s
You can implement missing steps with the snippets below:
#Given("^PS Restaurants web page is open$")
public void ps_Restaurants_web_page_is_open() throws Exception {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#When("^User clicks on Home Delivery button$")
public void user_clicks_on_Home_Delivery_button() throws Exception {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#Then("^Home Delivery page should load$")
public void home_Delivery_page_should_load() throws Exception {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
I just copied and pasted the output into my step definitions class and it still won't recognize the step definitions class. Earlier it was saying that there was no matching glue code for each step in the scenario, but that went away after I closed and reopened the feature file.
You can update your runner class as below:
package runner;
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
import cucumber.api.CucumberOptions;
#RunWith(Cucumber.class)
#CucumberOptions(
features= {"src/features"}, //It will pick all the feature files located in this folder
glue= {"stepDefinitions"} //Only package name is required here.
)
public class Runner {
}
Related
I have a problem with TestNG. I cannot run a test.
I am getting this error:
POM.xml has no errors.
Here is the code in test page:
import Pages.SearchPage;
import org.testng.annotations.Test;
import core.Web.AllListeners.*;
public class Search extends Listener {
#Test(groups = "Regression")
public void ticketBookingFunctionality() {
new SearchPage()
.openUrl()
.inputCaption("Comic")
.selectCityByValue()
.inputDateFrom("2020-01-01")
.inputDateTo("2021-07-05")
.clickButtonSearch()
.clickButtonBuy()
.chooseTicket()
.choosePrice()
.pushButtonFindTickets()
.closeLoginPopup();
}
}
Where can be the problem?
You need to add the following testng related jar files within your project.
I am new to automation testing and I am currently trying to automate a login form, user name tomsmith and pass supersecret password
However when i run my below script i only get to the login page, I am unsure why it is not inputing the details. I also would like to logout automatically as well.
package Form;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Authentication {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//http://tomsmith:SuperSecretPassword!
driver.get("http://tomsmith;SuperSecretPassword!#the-internet.herokuapp.com/login");
String pageMessage = driver.findElement(By.cssSelector("p")).getText();
System.out.println(pageMessage);
}
}
Pass your values to the input tags.
driver.findElement(By.ID('username')).sendKeys("tomsmith");
driver.findElement(By.ID('password')).sendKeys("SuperSecretPassword!");
I'm using Selenium with Cucumber along with Gradle and TestNG. The same scenario runs for multiple parameters (Examples). Issue I'm facing is that for the first assertion success, browser (driver) closes. But for subsequent assertions failures, browser (driver) does not close, instead a new browser instance is launched for next set of values.
My Feature file
Feature: Using Contact Form
To test the functionality of contact form
Scenario Outline: Filling contact form
Given I am on Home Page of "http://room5.trivago.com/contact/"
And Dismiss cookies popup
When I enter message as "<message>"
And I enter full name as "<fullname>"
And I enter email as "<email>"
And I click on Submit button
Then I see success message
Examples:
|message|fullname|email|
|just some gibberish message|Ashish Deshmukh|ashish#deshmukh.com|
| |Ashish Deshmukh|ashish#deshmukh.com|
|just some givverish message| |ashish#deshmukh.com|
|just some gibberish message|Ashish Deshmukh| |
My StepDefination file
package stepDef;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.Assert;
import pageObjectModels.ContactPageObjectModel;
import pageObjectModels.CookiesNoticePageObjectModel;
import java.util.logging.Logger;
public class Contact_Form extends Test_Base{
public static WebDriver driver;
public static ContactPageObjectModel objContact;
public static CookiesNoticePageObjectModel objCookies;
static Logger log = Logger.getLogger("com.gargoylesoftware");
#Given("^I am on Home Page of \"([^\"]*)\"$")
public void i_am_on_Home_Page_of(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.setProperty("webdriver.chrome.driver", "D:\\Selenium Webdriver/chromedriver.exe");
driver = new HtmlUnitDriver(true);
driver = new ChromeDriver();
driver.get(arg1);
objContact = new ContactPageObjectModel(driver);
objCookies = new CookiesNoticePageObjectModel(driver);
}
#Given("^Dismiss cookies popup$")
public void dismiss_cookies_popup() throws Throwable {
// Write code here that turns the phrase above into concrete actions
objCookies.acceptCookies();
}
#When("^I enter message as \"([^\"]*)\"$")
public void i_enter_message_as(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
objContact.enterMessage(arg1);
}
#When("^I enter full name as \"([^\"]*)\"$")
public void i_enter_full_name_as(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
objContact.enterFullName(arg1);
}
#When("^I enter email as \"([^\"]*)\"$")
public void i_enter_email_as(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
objContact.enterEmail(arg1);
}
#When("^I click on Submit button$")
public void i_click_on_Submit_button() throws Throwable {
// Write code here that turns the phrase above into concrete actions
objContact.clickSubmit();
}
#Then("^I see success message$")
public void i_see_success_message() throws Throwable {
// Write code here that turns the phrase above into concrete actions
String status = objContact.getSuccessStatus();
Assert.assertEquals(status, "Success");
driver.quit();
}
}
My build.gradle to run the test
group 'com.seleniumtestcucumber.mytest'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'idea'
configurations {
cucumberRuntime.extendsFrom testRuntime
}
task cucumber() {
dependsOn assemble, compileTestJava
doLast {
javaexec {
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--plugin', 'pretty', '--glue', 'stepDef', 'src/test/java']
}
}
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile 'org.seleniumhq.selenium:selenium-server:2.44.0'
// https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.4.0'
compile 'org.testng:testng:6.1.1'
// https://mvnrepository.com/artifact/info.cukes/cucumber-testng
compile group: 'info.cukes', name: 'cucumber-testng', version: '1.2.5'
// https://mvnrepository.com/artifact/info.cukes/cucumber-java
compile group: 'info.cukes', name: 'cucumber-java', version: '1.2.5'
}
Console Log
Feature: Using Contact Form
To test the functionality of contact form
Scenario Outline: Filling contact form # features/Contact_Form.feature:5
Given I am on Home Page of "http://room5.trivago.com/contact/"
And Dismiss cookies popup
When I enter message as "<message>"
And I enter full name as "<fullname>"
And I enter email as "<email>"
And I click on Submit button
Then I see success message
Examples:
Starting ChromeDriver 2.29.461591 (62ebf098771772160f391d75e589dc567915b233) on port 42643
Only local connections are allowed.
Aug 10, 2017 4:21:10 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Accepted cookies notice.
Entered message: just some gibberish message
Entered name: Ashish Deshmukh
Entered email: ashish#deshmukh.com
Clicked on Submit button.
Success
Scenario Outline: Filling contact form # features/Contact_Form.feature:15
Given I am on Home Page of "http://room5.trivago.com/contact/" # Contact_Form.i_am_on_Home_Page_of(String)
And Dismiss cookies popup # Contact_Form.dismiss_cookies_popup()
When I enter message as "just some gibberish message" # Contact_Form.i_enter_message_as(String)
And I enter full name as "Ashish Deshmukh" # Contact_Form.i_enter_full_name_as(String)
And I enter email as "ashish#deshmukh.com" # Contact_Form.i_enter_email_as(String)
And I click on Submit button # Contact_Form.i_click_on_Submit_button()
Then I see success message # Contact_Form.i_see_success_message()
Starting ChromeDriver 2.29.461591 (62ebf098771772160f391d75e589dc567915b233) on port 11801
Only local connections are allowed.
Aug 10, 2017 4:21:46 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Accepted cookies notice.
Entered message:
Entered name: Ashish Deshmukh
Entered email: ashish#deshmukh.com
Clicked on Submit button.
Error
Scenario Outline: Filling contact form # features/Contact_Form.feature:16
Given I am on Home Page of "http://room5.trivago.com/contact/" # Contact_Form.i_am_on_Home_Page_of(String)
And Dismiss cookies popup # Contact_Form.dismiss_cookies_popup()
When I enter message as "" # Contact_Form.i_enter_message_as(String)
And I enter full name as "Ashish Deshmukh" # Contact_Form.i_enter_full_name_as(String)
And I enter email as "ashish#deshmukh.com" # Contact_Form.i_enter_email_as(String)
And I click on Submit button # Contact_Form.i_click_on_Submit_button()
Then I see success message # Contact_Form.i_see_success_message()
java.lang.AssertionError: expected [Success] but found [Error]
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:513)
at org.testng.Assert.assertEqualsImpl(Assert.java:135)
at org.testng.Assert.assertEquals(Assert.java:116)
at org.testng.Assert.assertEquals(Assert.java:190)
at org.testng.Assert.assertEquals(Assert.java:200)
at stepDef.Contact_Form.i_see_success_message(Contact_Form.java:72)
at ✽.Then I see success message(features/Contact_Form.feature:12)
Starting ChromeDriver 2.29.461591 (62ebf098771772160f391d75e589dc567915b233) on port 22809
Only local connections are allowed.
Aug 10, 2017 4:22:39 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Kindly suggest how to over come this. Is there a way I can use #BeforeTest #AfterTest in this?
You should read about cucumber hooks in details.
For your specific problem, you can use #After and #Before. This works for Junit Runner, never tested with TestNG but should work I guess as hooks are part of Cucumber and not JUnit/TestNG.
import cucumber.annotation.After;
import cucumber.annotation.Before;
#Before
public void beforeScenario()
{
System.setProperty("webdriver.chrome.driver", "D:\\Selenium Webdriver/chromedriver.exe");
driver = new HtmlUnitDriver(true);
driver = new ChromeDriver();
}
#After
public void afterScenario()
{
driver.quit()
}
I am trying to run the below code but in vain.
Code is not compiling and giving error as "selenium cannot be resolved".
Can anyone look into the below code -
package com.example.tests;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
public class prashantk {
#Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://book.theautomatedtester.co.uk/");
selenium.start();
}
#Test
public void testAuto_1() throws Exception {
selenium.open("/chapter2");
verifyEquals("Button with name", selenium.getValue("name=but2"));
verifyEquals("chocolate", selenium.getValue("xpath=(//input[#name='verifybutton'])[2]"));
selenium.click("link=Index");
selenium.waitForPageToLoad("60000");
verifyTrue(selenium.isTextPresent("Chapter4"));
}
#After
public void tearDown() throws Exception {
selenium.stop();
}
}
You have references to a field named selenium, but there is no such field defined.
Somewhere in your class, probably on a line just before the #Before, you want to add this field:
Selenium selenium;
Apparently from comments you also don't have the methods verifyEquals and verifyTrue referenced in your code. Those methods are defined in a base class SeleneseTestCase which your test should extend:
public class prashantk extends SeleneseTestCase {
Replace the below line in your code as shown below
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://book.theautomatedtester.co.uk/");
to
DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://book.theautomatedtester.co.uk/");
Looking at code gives an impression it was done in Selenium IDE first the exported to a tool like eclipse.
I really don't think 'verifyEquals' exist in WebDriver instead use JUnit assertions or Hamcrest assertions. Try to write your code from scratch in Java its a lot less hassle.
I use Selenium Web Driver with JUnit in Eclipse. I want diminish my code by creating new classes for repitable steps. I want store this classes in separate files for convenience. For example this one of such classes:
import org.openqa.selenium.ie.InternetExplorerDriver;
//Login
public class Login {
private InternetExplorerDriver driver;
String url;
String name;
String password;
String language;
public Login (String ur, String nam, String pass, String lang){
url=ur;
name=nam;
password=pass;
language=lang;
}
public void log_in (){
driver.get(url);
driver.findElement(By.name("username")).sendKeys(name);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.name("language")).sendKeys(language);
driver.findElement(By.name("logon_action")).click();
}
}
This is my main test:
package bobsworld;
import init.Login;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class WebTest {
private WebDriver driver;
#Test
public void testUnit() throws Exception {
//Open new window
System.setProperty("webdriver.ie.driver", "IEDriverServer.exe");
driver = new InternetExplorerDriver();
//Login
init.Login login = new init.Login ("myurl", "log","pas","English");
login.log_in();
}
The problem is with object driver. I get java.lang.NullPointerException and can't execute test. How should I organize call to Login and code to make my test work?
What you try to achieve is a quite popular approach called "PageObjects".
Your issues:
Use some checkstyle tool to improve code quality
Do a whole lot of research about Java in general
The Login Class or "init.Login" (it's quite unusual to have a part of the package as prefix for the class) is not able to use the same WebDriver instance because you don't forward it. Normally you would have opened a second WebDriver instance but that seems to be impossible though I don't know your setup. You have to forward the "driver" as a parameter to the constructor of the Login class.
Try to use real PageObjects as described here inluding PageFactory to improve readability and maintainability