2

I have a strange behavior : I created Test Class with 2 Test Methods as below.

When I call AddLimitedBuyOrderwithRandom in the first method it works fine without any exception, but if I call AddLimitedBuyOrderwithRandom from the second method I get a NoSuchElement exception.

This is the code for the test class:

public class TestLoginPage extends TestBase {
    public HomePage homeobject;
    public NewOrdersLoginPage OrderLoginObject;
    public OrderMgmtPage OrdersMgmentObject;

    @Test
    public void SuccessLogin() throws InterruptedException
    {
        homeobject = new HomePage(driver);
        homeobject.OpenOrdersPage();
        OrderLoginObject = new NewOrdersLoginPage(driver);
        OrderLoginObject.userLogin("140", "12345");
        OrdersMgmentObject = new OrderMgmtPage(driver);
        OrdersMgmentObject.AddLimitedBuyOrderwithRandom("10");
    }

    @Test(enabled= true)
    public void AddLimitOrderTest() throws InterruptedException
    {
        OrdersMgmentObject = new OrderMgmtPage(driver);
        OrdersMgmentObject.AddLimitedBuyOrderwithRandom("10");
    }   
}

and this is the code for AddLimitedBuyOrderwithRandom:

public void AddLimitedBuyOrderwithRandom(String SharesCount) throws InterruptedException
{  
    clickButton(NewOrderLink);
    clickButton(BuyTypeRadio);
    selectDropdownByValue( companyList, "Company");
    typeText(ShareNoTxt, SharesCount);
    selectDropdownByValue(  OrderTypeList, "2");
    selectDropdownByValue( OrderValidityList, "0");
    double Pricerandom = getRandomNumberInRange(ReturnShareLimitDown(),ReturnShareLimitUp());
    typeText(SharePriceTxt,Double.toString(Pricerandom));
    clickButton(SubmitOrderBtn);    
} 

(Added from comments on answers) My base class is:

public class TestBase 
{ 
    public static WebDriver driver ; 
    public static String downloadPath = System.getProperty("user.dir") + "\\Downloads"; 
    public String BaseURL = "URL"; 

    @BeforeSuite 
    @Parameters({"browser"}) 
    public void startDriver(@Optional ("chrome") String browserType) 
    { 
        if (browserType.equalsIgnoreCase("chrome") ) 
        { 
            System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"/drivers/chromedriver.exe"); 
            driver = new ChromeDriver(); 
        } 
    } 
    
    driver.manage().window().maximize(); 
    driver.navigate().to(BaseURL); 
} 
1
  • 1
    In your first test, you log on before going to the order management page. In the second test, you don't - is it possible that not being logged in is the problem?
    – Kate Paulk
    Nov 13 '18 at 13:37
0

Many things are hidden on the code. It can be anything on your code or on the application logic.

But most likely is that you have just performed login once. When you open the browser on the second test, Selenium cleans everything.

You would have to login again or save some cookie, using a @BeforeSuite method.

1
  • I Already use @BeforeSuit : how to save Cookies
    – Amr Shehab
    Nov 14 '18 at 9:36
0

Try to add wait before interacting with your element with waitForVisible.

E.g:

 NewOrderLink.waitForVisible(1000);
 clickButton(NewOrderLink);

Because on your first method, you have prior steps. Unlike in second step that you call it right away.

0

Here you are neither quitting the driver after test nor you logging again in the script. So you are using the same logged in instance in the second test. Selenium doesn't cleans anything until you are using driver.quit or close.

So here if you are running second method directly before running the first one it will definitely fail and will throw the error because it is expecting a user logged in window. You have to run the whole file and adding priority=1 to the first method and priority=2 to the second one.

or what you can do is login to the application in before method like:

public class TestLoginPage extends TestBase {
    public HomePage homeobject;
    public NewOrdersLoginPage OrderLoginObject;
    public OrderMgmtPage OrdersMgmentObject;

    @BeforeMethod
    public void SuccessLogin() throws InterruptedException
    {
        homeobject = new HomePage(driver);
        homeobject.OpenOrdersPage();
        OrderLoginObject = new NewOrdersLoginPage(driver);
        OrderLoginObject.userLogin("140", "12345");
    }

 @Test(enabled= true)
    public void AddLimitOrderTest1() throws InterruptedException
{

    OrdersMgmentObject = new OrderMgmtPage(driver);
    OrdersMgmentObject.AddLimitedBuyOrderwithRandom("10");
}   

@Test(enabled= true)
public void AddLimitOrderTest2() throws InterruptedException
{
    OrdersMgmentObject = new OrderMgmtPage(driver);
    OrdersMgmentObject.AddLimitedBuyOrderwithRandom("10");
}   

We as a company are providing automation testing services. We need to take care of the small things while developing a framework. Also many other things we can do is we can also make a user login in the @BeforeSuite.

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.