I created two tests. Pulled up BeforeSuite and AfterSuite in superclass method.
Tests passes individually but fails when I execute them as a package.
Error NullPointerException at string
driver.get(baseUrl + "test/test/");
public class MyTestBase {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
#BeforeSuite
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://test.com/";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#AfterSuite
public void tearDown() throws Exception {
driver.quit();
}
Code:
private void login() throws InterruptedException {
driver.get(baseUrl + "test/test/");
driver.findElement(By.xpath("//input")).clear();
driver.findElement(By.xpath("//input")).sendKeys("user234");
driver.findElement(By.xpath("//div[2]/div[2]/div/input")).clear();
driver.findElement(By.xpath("//div[2]/div[2]/div/input")).sendKeys("645637");
submitButton();
Thread.sleep(2000);
}
Test 1
`public class AddEquipment extends MyTestBase { }`
Test 2
`public class AddAnotherEquipment extends MyTestBase { }`
Both tests are the same, but with different parameters Equipment and Description fields.
I am at the beginning за learning WebDriver and tried to create something simple.
Code for test 1:
package com.example.tests;
import org.testng.annotations.Test;
public class AddEquipment extends MyTestBase {
#Test
public void testAddEquipment() throws Exception {
GroupData group = new GroupData("Equipment", "Description");
createEquipment(group);
// assert
}
protected void createEquipment(GroupData group) throws InterruptedException {
login();
menuClickEquipment();
clickAddButton();
fillOutForm(group);
submitButton();
logout();
}
private void login() throws InterruptedException {
driver.get(baseUrl + "test/test/");
driver.findElement(By.xpath("//input")).clear();
driver.findElement(By.xpath("//input")).sendKeys("user234");
driver.findElement(By.xpath("//div[2]/div[2]/div/input")).clear();
driver.findElement(By.xpath("//div[2]/div[2]/div/input")).sendKeys("645637");
submitButton();
Thread.sleep(2000);
}
Related
I am new to Selenium, so basically whenever I ran my test, it would open up the URL which I have mentioned in my test. In my same Test I have also mentioned to fill the username and Password.
But somehow once the browser gets launched and redirect to the URL, it opens up another instance of blank browser failing my script that element not found.
Please help me here.
///////////////////////////////////////////////////////////////////////////
public class TruefillTest extends BaseClass {
public Truefill truefill()
{
WebDriver driver=InitializeDriver();
return new Truefill(driver);
}
#Test
public void userLoginIntoTheSystem()
{
truefill().dashBoard().Url();
truefill().dashBoard().EnterUsername("bjdgfe#swcwr.com");
truefill().dashBoard().EnterPassword("Test1234");
}
///////////////////////////////////////////////
public class Truefill {
private WebDriver driver;
public Truefill(WebDriver driver) {
this.driver=driver;
}
public DashBoardPage dashBoard()
{
return new DashBoardPage(driver);
}
////////////////////////////////////////////////////////////
public class DashBoardPage {
private final WebDriver driver;
By Username= By.xpath("//input[#name='name']");
By Password= By.xpath("//input[contains(#id,'exampleInputPassword1')]");
public DashBoardPage(WebDriver driver) {
this.driver=driver;
}
public void Url()
{
driver.get("https://rahulshettyacademy.com/angularpractice/");
}
public void EnterUsername(String username)
{
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(Username).sendKeys(username);
}
public void EnterPassword(String password)
{
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(Password).sendKeys(password);
}
////////////////////////////////////////////////////////////
public class BaseClass {
WebDriver driver;
public WebDriver InitializeDriver()
{
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
return driver;
}
}
Each call to the truefill() method initializes a new instance of WebDriver. Since your test is calling it multiple times, it will start a new browser instance on each line. Instead, store the DashboardPage in a local variable:
#Test
public void userLoginIntoTheSystem() {
DashBoardPage dashBoardPage = truefill().dashBoard();
dashBoardPage.Url();
dashBoardPage.EnterUsername("bjdgfe#swcwr.com");
dashBoardPage.EnterPassword("Test1234");
}
You might also want to use a setup method to initialize the Truefill instance, rather than creating it on demand:
private Truefill truefill;
#BeforeEach
public void initializeTruefill() {
WebDriver driver = InitializeDriver();
truefill = new Truefill(driver);
}
#Test
public void userLoginIntoTheSystem() {
DashBoardPage dashBoardPage = truefill.dashBoard();
dashBoardPage.Url();
dashBoardPage.EnterUsername("bjdgfe#swcwr.com");
dashBoardPage.EnterPassword("Test1234");
}
This assumes JUnit 5. If you're using JUnit 4, the annotation is #Before instead of #BeforeEach.
Selenium - I have defined my driver in a singleton class and used that driver in multiple classes. Now if i run the classes in series it's working fine, But if i try to run the classes in parallel it's not working.
For eg : if i give the thread count as 2 only one browser is opened.
How to run the testcases in parallel using singleton class?
Singleton class :
public class SingletonDesignPattern {
private static Utilities obj;
private SingletonDesignPattern() {
}
public static Utilities getInstance() {
if(obj == null) {
obj = new Utilities();
}
return obj;
}
Driver class:
public class Driver {
public static WebDriver driver;
public static Utilities util = SingletonDesignPattern.getInstance();
public String currentdate = null;
Test_action test = new Test_action();
#BeforeSuite
public void execute_test() throws Exception {
try {
driver = util.initializeDriver();
}
catch (Exception e) {
e.printStackTrace();
}
}
#BeforeTest
public void beforebacklog() throws Exception
{
util.getlogin();
}
#AfterSuite
public void quit() throws Exception {
driver.quit();
}
}
I have 3 classes like below but when I try to run my test I get NullPointer (TestPage->input1). On debug I spotted that I have a second instantion of driver which is null. Can anyone help me what I did wrong and how to fix it? Shouldn't it work properly?
public class BaseScenario {
protected WebDriver driver;
#BeforeMethod
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Ed\\Desktop\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://toolsqa.com/automation-practice-form/");
}
#AfterMethod
public void TearDown() {
driver.quit();
}
}
'
public class TestsPage {
WebDriver driver;
public TestsPage(WebDriver driver)
{
this.driver=driver;
}
public void input1(){
driver.findElement(By.xpath("//*[#id='content']/form/fieldset/div[1]/input[1]")).sendKeys("Kuba");;
}
public WebElement input2(){
WebElement input2 = driver.findElement(By.xpath("//*[#id='content']/form/fieldset/div[1]/input[2]"));
return input2;
}
}
'
public class Tests extends BaseScenario{
TestsPage pagee = new TestsPage(driver);
#Test
public void TC1() throws Exception {
pagee.input1();
pagee.input2().sendKeys("Chudy");
}
}
I am new in JUnit Selenium, and I found problem.
I cannot find elements using locator in #Test method. I dont have predictive search when I type driver. I can if I type into #Before.
E.g I cant type
#Test
..
driver.findElement(By.id("gs_htif0")).sendKeys("blabla");
My class contains -
#Before
public void setUp() throws Exception {
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
driver = new FirefoxDriver();
String baseURL = "https://www.google.com";
driver.get(baseURL);
}
#Test
public void test() {
driver.**___PROBLEM___**
}
#After
public void tearDown() throws Exception {
}
That's because you've declared
WebDriver driver;
locally within the setUp method which is also annotated using #Before in your case.
You shall move this to the class level and use further as -
public class SomeTest {
WebDriver driver;
#Before
public void setUp() throws Exception {
...
driver = new FirefoxDriver();
...
driver.get(baseURL);
}
#Test
public void test() {
driver.getTitle(); //just an example
}
.... // other methods
}
I am trying to create a framework to test cross-platform mobile web apps (non native). Here is how my code is setup:
Test Class:
public class TestSuite extends MobileLibrary{
#BeforeClass
public static void setUpTests() throws Exception {
setUp();
}
#AfterClass
public static void cleanUpTests() throws Exception {
driver.quit();
}
#Test
public void validateSignIn() throws Exception
{
String username = "testtest";
String password = "testtest";
SignInMobile(driver,username,password);
assertTrue(true);
}
#Test
public void randomTests() throws Exception
dbcSelector(driver,"test");
assertTrue(true);
}
}
Base Class
public class SetupBase
{
protected static AppiumDriver driver;
protected static AppiumPlatform appiumPlatform;
protected static DeviceSize deviceSize;
protected static DeviceName deviceName;
protected static String deviceID;
protected static DesiredCapabilities capabilities;
protected static enum AppiumPlatform{
DESKTOP,IOS,ANDROID
}
protected static enum DeviceSize{
SMALL,MEDIUM,LARGE
}
protected static enum DeviceName{
NEXUS7,S5,IPHONE5S
}
public static void setUp() throws MalformedURLException /*throws Exception*/ {
String deviceNameEnv = System.getenv("DEVICENAME");
switch(deviceNameEnv){
//sets devicename here
}
//sets platform here
if(deviceName==DeviceName.NEXUS7 || deviceName==DeviceName.S5)
appiumPlatform = AppiumPlatform.ANDROID;
else if(deviceName==DeviceName.IPHONE5S)
appiumPlatform = AppiumPlatform.IOS;
else
appiumPlatform = AppiumPlatform.DESKTOP;
switch(deviceName){
//sets size here
}
capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName",deviceID);
if(appiumPlatform==AppiumPlatform.ANDROID){
capabilities.setCapability("platformName","Android");
capabilities.setCapability("browserName","Chrome");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
else if(appiumPlatform==AppiumPlatform.IOS){
capabilities.setCapability("platformName","iOS");
capabilities.setCapability("browserName","Safari");
driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
}
}
No matter what I do, any time I try to use driver in the second test I will get a java null pointer exception. MobileLibrary extends the baseclass and contains all my WebDriver element functions.
Edit: If I directly do driver.findelement in my second test I get an element not found exception. It looks like something is being reset after the end of a #test and I have no idea what.
I found out that the problem was actually with the #test order execution. I didn't realize that junit doesn't run tests in order, I'll be looking into the test suite fixtures.