1

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
    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.

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

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.