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.
Related
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.
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();
I was trying Selenium automation testing in https://www.yatra.com/etw-desktop/ . My objective was to click an Image Button named 'Asia' which will redirect to another page (Images attached). I copied the full XPath and tried but I am getting a NullPointerException. Please give some suggestions since I didn't find anything wrong with my code.
package com.stackroute.SeleniumProject;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
/**
* JUnit project.
*/
public class Yatra {
public static WebDriver driver = null;
#FindBy(xpath = "/html/body/my-app//app-drawer-layout/app-header-layout/iron-pages/my-home//div/div/div/div/paper-material[2]/div[1]/div/a[2]/div[4]")
WebElement Asia;
#BeforeClass
public static void setup() {
String chromePath = System.getProperty("user.dir") + "/lib/chromedriver.exe";// directory of chrome driver
System.setProperty("webdriver.chrome.driver", chromePath);
driver = new ChromeDriver();
}
#AfterClass
public static void close() throws InterruptedException {
driver.close();
}
#Test
public void test1() throws InterruptedException {
driver.manage().window().maximize();
driver.get("https://www.yatra.com/etw-desktop/");
driver.manage().timeouts().implicitlyWait(4000, TimeUnit.MILLISECONDS);
Thread.sleep(5000);
// the error is in the below line Asia.click()
Asia.click();
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
Thread.sleep(2000);
Assert.assertEquals("page not found", "https://www.yatra.com/etw-desktop/city-list", driver.getCurrentUrl());
}
}
Image showing the element to be clicked
Image of web page after click operation
You are using PageFactory(from page object model), so you will need to init() the webelements.
For this you will need to import
org.openqa.selenium.support.PageFactory;
An before starting the test, you will need to initialise the elements:
PageFactory.initElements(driver, this) // where you pass the driver and this class to know which webelements to start.
You can take a look here to understand the approach and another POM framework easier to understand TUTORIAL
Here i am trying to execute test cases in same class with only one browser instance. But struck here. How can i refresh and come back to same page to execute further cases of same classes.If i execute the cases in different classes, they are executing fine but giving error when executing in same class.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Parallel {
Parallel objectb;
WebDriver driver;
public Parallel(WebDriver driver) {
this.driver=driver;
// TO DO Auto-generated constructor stub
}
public void Open(WebDriver driver) {
this.driver=driver;
// TO DO Auto-generated constructor stub
}
#BeforeClass
public void beforeclass() {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+".\\drivers\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.browserstack.com/users/sign_up");
}
#Test
public void testOnChromeWithBrowserStackUrl() throws InterruptedException {
Open(driver);
Thread.sleep(2000);
driver.manage().window().maximize();
driver.findElement(By.id("user_full_name")).sendKeys("Mamta Singh");
driver.findElement(By.id("user_email_login")).sendKeys("mamtasingh24#gmail.com");
driver.findElement(By.id("user_password")).sendKeys("browserstack");
System.out.println(
"this is the test related to chrome browserstack homepage" + " " + Thread.currentThread().getId());
}
#Test
public void testOnChromeWithBrowserStackSignUp() throws InterruptedException
{
objectb= new Parallel(driver);
Thread.sleep(2000);
driver.manage().window().maximize();
driver.findElement(By.id("user_full_name")).sendKeys("Sadhvi Singh");
driver.findElement(By.id("user_email_login")).sendKeys("sadhvisingh24#gmail.com");
driver.findElement(By.id("user_password")).sendKeys("browserstack");
System.out.println("this is the test related to chrome browserstack login"+ " " +Thread.currentThread().getId());
}
#AfterClass
public void close()
{
driver.quit();
}
}
You need a standard constructor in your test class.
public class Parallel {
public Parallel() {
// Do something
}
...
}
BTW: There are a few things in your code that do not make sense.
You have a constructor and a public method Open with a WebDriver argument but you are initializing the driver in the beforeclass anyway. So you could remove the constructor and the Open method.
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.