1

Here are the details:

Question 1 : When I run my test, locators are not identified from the locators file? why? But when I added the driver.findElement(By.) in my test , it identifies though.

Question 2 : From my base class , I see 2 chrome browsers opened while running my tests? what changes are needed to fix that?

I have the below files :

Base.java - where my chrome driver is instantiated

public abstract class Base {

 @BeforeClass
 public static void setUp() {
  System.setProperty("webdriver.chrome.driver", path);
  WebDriver driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 }

 @AfterClass
 public static void tearDown() {
  System.setProperty("webdriver.chrome.driver", path);
  WebDriver driver = new ChromeDriver();
  driver.close();
  driver.quit();
 }

}

locators.java: Contains pagelevel element locators

public class sockLocators {
    // header text
    @FindBy(xpath = "//h1")
    public static WebElement socksHeader;
}

TestCaseclass.java

@Test
    public class SockTests extends Base {    
     WebDriver driver = new ChromeDriver();

    @Test
   public void verifyTitle() throws InterruptedException {    
        JungleSockCheckoutPage.navigateTo(driver);  
        //does not identify my locator  
        Assert.assertTrue(CheckoutPage.isPageTitleCorrect(driver));    
}

Checkoutpage.java

//verify page title
    public static boolean isPageTitleCorrect(WebDriver driver){
        if(driver.getTitle().contains(JS_CHECKOUT_PAGE_TITLE)){
            return true;
        }    
        else{    
            return false;
        }    
    }

Can you please help?

  • 1
    could you format your question so that we could read your code more easily? Also what do you mean by locators file? – Alexey R. Apr 21 at 8:54
  • 1
    Why are you creating driver in after class , remove that. Define driver as a global variable – PDHide Apr 21 at 10:49
  • You have created multiple instance of webdriver. Instead of creating multiple webdriver create singleton class and use the instance of singleton every where you want. – Ankit jain Apr 23 at 6:34
  • @PDHide - If I define as a global variable instead of in AfterClass - it throws and error saying that System.setproperty cannot be identified, hence I had to chage it as localvariable in before and afterclass. Is there any other way to do it? Please let me know. Thanks in advance. @ Alexey R - Locators file is nothing but my page object file where all my webelements are defined page wise. OK.. will try using a singleton class and see if that helps!! Will keep you posted. Thanks all for the reply. – Seluser Apr 24 at 19:36
  • @Seluser public static webdriver driver = null – PDHide Apr 24 at 19:40
3
  1. If you want to use the PageFactory @FindBy annotation you also need to initialize them with PageFactory.initElements(driver, this);.

  2. Actually you create 3 browsers, but one gets closed instantly in tearDown. Everytime you use WebDriver driver = new ChromeDriver(); it starts a new browser. You should store 1 driver instance as a global variable or in your test-context and reuse that everywhere else.

See the example here: https://www.pluralsight.com/guides/getting-started-with-page-object-pattern-for-your-selenium-tests

| improve this answer | |
  • I initialized using PageFactory.initElements(driver, this); – Seluser Apr 24 at 19:35
  • +1 - and to be clear - a new browser window for each test is the expected, default and desired behavior – Michael Durrant Apr 24 at 23:17
  • Otherwise tests start interacting with each and you get dependency hell. YOu have to address setup, but you have to do it within each test – Michael Durrant Apr 24 at 23:19
0

@PDHide - If I define as a global variable instead of in AfterClass - it throws and error saying that System.setproperty cannot be identified, hence I had to chage it as localvariable in before and afterclass. Is there any other way to do it? Please let me know. Thanks in advance.

@ Alexey R - Locators file is nothing but my page object file where all my webelements are defined page wise.

OK.. will try using a singleton class and see if that helps!! Will keep you posted. Thanks all for the reply.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.