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
Related
I have defined the following acceptance test scenario using selenium webdriver and junit which runs perfectly fine when executing the test with junit:
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import java.util.*;
public class SuccessfulLogin {
private WebDriver driver;
private Map<String, Object> vars;
JavascriptExecutor js;
#Before
public void setUp() {
System.setProperty("webdriver.chrome.driver", "C:\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
}
#After
public void tearDown() {
driver.quit();
}
#Test
public void firsttest() {
driver.get("http://localhost:9000/friendsify/login");
driver.manage().window().setSize(new Dimension(2576, 1408));
driver.findElement(By.name("username")).click();
driver.findElement(By.name("username")).sendKeys("max#mustermann.de");
driver.findElement(By.name("password")).click();
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.name("password")).sendKeys(Keys.ENTER);
driver.findElement(By.linkText("Friends")).click();
driver.close();
}
}
What is the proper way to make use of this test for testing the performance with JMeter?
First of all, are you aware of WebDriver Sampler plugin? Theoretically it can make your life easier.
Compile your test as .jar file
Put it under "lib/junit" path of your JMeter installation
Put all it's dependencies like selenium java library to "lib" folder of your JMeter installation
Restart JMeter to pick up the .jar
Your test class and method should be visible in JUnit Request sampler
More information: How to Use JUnit With JMeter
Be aware that browsers are very resource intensive, i.e. Firefox 96 requires 1 CPU core and 2 GB of RAM so you might need a powerful machine to conduct the load tests. So maybe it worth considering using HTTP Request samplers for load testing your application.
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!");
below is the code : im not able launch the browser using the code . please suggest the solution
import org.openqa.selenium.Test;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.*;
public class NewTest {
public WebDriver driver;`enter code here`
#Test
public void VerifyHomepageTitle() {
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://google.com");
driver.quit();
}
}
You are closing the web page immediately after opening using 'driver.quit()'. Comment this code and try you will see the difference.
Hello I am new to automation and i have tried automating dropbox.com sign in but my code is failing after clicking on sign in link. i am not able to pass the values (username and password) in the frame of the sign in box. Following is my code.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Drop_box {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("http://www.dropbox.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[#id='sign-in']")).click();
driver.findElement(By.xpath("//*[#id='pyxl1851']")).sendKeys("123#gmail.com");
driver.findElement(By.xpath("//*[#id='pyxl1854']")).sendKeys("1234");
driver.findElement(By.xpath("//*[#id='regular-login-forms']/form[1]/div[3]/button")).click();
}
}
This should work:
driver.findElement(By.cssSelector("a#sign-in")).click();
driver.findElement(By.cssSelector("div#index-sign-in-modal input[name='login_email']")).sendKeys("email");
driver.findElement(By.cssSelector("div#index-sign-in-modal input[name='login_password']")).sendKeys("password");
driver.findElement(By.cssSelector("div#index-sign-in-modal div.sign-in-text")).click();
If you prefer xpath over css selectors, then use following lines of code:
driver.findElement(By.xpath("//*[#id='sign-in']")).click();
driver.findElement(By.xpath("//div[#id='index-sign-in-modal']//input[#name='login_email']")).sendKeys("email");
driver.findElement(By.xpath("//div[#id='index-sign-in-modal']//input[#name='login_password']")).sendKeys("password");
driver.findElement(By.xpath("//div[#id='index-sign-in-modal']//div[#class='sign-in-text']")).click();
Hi I am learning selenium grid for the first time.I am unable to understand why Desired capabilities class is used to set browser name and platform details in a remote machine.
please let me know if there is any proper documentation on it apart from google-wiki page
Thanks
prathima
Not all remote machines (or sessions) support the features requested by a user. E.g. user may want to use Chrome on Windows but some machines are only running Firefox on Linux.
The desiredCapabilities is used to describe the features of a session requested by user. You can refer to this link and this link for more info.
- when user send request to remote machine to run a particular test on
a particular browser on a particular platform.
- It doesn't mean that all the remote machine will support all the
features requested by user such as (Particular browser on a
particular machine).
**Example:**
- If user request to execute a testcase on internet explorer on Linux
platform. It doesn't mean that always Linux machine has internet
explorer it would have firefox not internet Explorer(Because it is
open source).
- Linux machine doesn't support the features requested by a User. so
that, that point of time we will use DesiredCapabilites class to set
the platform , Browser Name and version of browser of Remote Machine
to execute the Test.
Example:
package grid;
import static org.junit.Assert.*;
import java.net.URL;
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.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class SimpleGridTest
{
WebDriver driver;
String baseUrl , nodeUrl;
#Before
public void setUp() throws Exception
{
WebDriver driver;
String baseUrl , nodeUrl;
baseUrl = "https://www.facebook.com";
nodeUrl = "http://192.168.10.21:5568/wd/hub";
DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setBrowserName("firefox");
capability.setPlatform(Platform.WIN8_1);
driver = new RemoteWebDriver(new URL(nodeUrl),capability);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.MINUTES);
}
#Test
public void test() throws InterruptedException
{
driver.manage().window().maximize();
driver.get("https://www.google.co.in");
driver.findElement(By.linkText("Gmail")).click();
driver.findElement(By.id("Email")).sendKeys("abc#gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("1234");
driver.findElement(By.id("signIn")).click();
}
#After
public void tearDown() throws Exception
{
driver.quit();
}
}