Unable to execute single tc, login then run other application features, right now its running as a suite



  • I have to execute my test cases as a single as well as a suite, right now its running as a suite but not as a single test case in selenium webdriver: Below is my test which are calling methods present in business class, and business class calling page class containing locators: Below is my test of one report, same way we have to create different reports:

    Business class:

        package com.deere.gmsr.business;
        import org.openqa.selenium.By;
        import org.openqa.selenium.JavascriptExecutor;
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.WebElement;
        import org.openqa.selenium.support.CacheLookup;
        import org.openqa.selenium.support.FindBy;
        import org.openqa.selenium.support.How;
        import org.openqa.selenium.support.ui.ExpectedConditions;
        import org.openqa.selenium.support.ui.Select;
        import org.openqa.selenium.support.ui.WebDriverWait;
    
        import com.deere.gmsr.page.MhCurrentMonthReportPage;
        import com.deere.gmsr.utility.PageUtility;
        import com.deere.gmsr.utility.ScreenshotUtility;
    
        public class MhCurrentMonthReportBusiness extends MhCurrentMonthReportPage {
    
            private WebDriver driver;
            //WaitUtility waitutility;
    
            public MhCurrentMonthReportBusiness(WebDriver driver) {
                super(driver);
                this.driver=driver;
    
            }
    
            public void clickToCreateNewButton() throws Exception {
    
                //  WaitUtility.waitForElement(createNewButton);
                //  WebDriverWait wait=new WebDriverWait(driver,10);
                //  WebElement createNewButton=wait.until(ExpectedConditions.elementIfVisible(By.xpath(PageUtility.CREATE_NEW_BUTTON_XPATH)));
                //  WebElement createNewButton=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(PageUtility.CREATE_NEW_BUTTON_XPATH)));
                    Thread.sleep(3000);
                    createNewButton.click();
                    System.out.println("fffff");
                    ScreenshotUtility.captureScreenShot(this.driver);
                } 
    
            public void selectEquipmentDivision() {
    
                 Select equipmentDivisionDropdown=new Select(driver.findElement(By.xpath(PageUtility.SELECT_EQUIPMENT_DIVISION)));
                 equipmentDivisionDropdown.selectByIndex(1);
                 System.out.println(equipmentDivisionDropdown.getFirstSelectedOption());    
                }
            public void openLocation() throws Exception{
                Thread.sleep(3000);
    
                   openLocationPopup.click();
            }
    
        public void selectGeography() throws Exception {
    
                Select selectGeographyType=new Select(driver.findElement(By.xpath(PageUtility.SELECT_GEAOGRAPHY_TYPE)));
                selectGeographyType.selectByIndex(1);
                Thread.sleep(3000);
                clickOnGreaterThanSign.click();
                clickOnNextLevel.click();
                clickToUS.click();
                clickOnSaveButton.click();
    
    
            }
    
        public void selectMarketSegment() throws Exception {
    
            Thread.sleep(3000);
            clickOnSelectButton.click();
            Thread.sleep(3000);
            clickOnAuction.click();
            Thread.sleep(3000);
            save1.click();
    
            }
    
        public void openProductWindow() throws Exception {
            //  WebDriverWait wait=new WebDriverWait(driver,30);
            //  WebElement openProductPopup=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(PageUtility.OPEN_PRODUCT_POPUP)));
                Thread.sleep(3000);
                openProductPopup.click();
                selectProduct.click();
                clickOnProductSaveButton.click();
            }
    
            public void selectHistoryReports() throws Exception {
            //  selectHistoryReports.isSelected();
                Thread.sleep(3000);
                selectHistoryReports.click();
    
                Thread.sleep(1000);
                //WebDriverWait wait=new WebDriverWait(driver,15);
                //WebElement selectMonthly=wait.until(ExpectedConditions.visibilityOf(selectMonthly));
                selectMonthly.click();      
            }
    
            public void selectTimePeriod(){
                Select currentMonth= new Select(driver.findElement(By.xpath(PageUtility.SELECT_TIMEPERIOD)));
                currentMonth.selectByVisibleText("Current Month");
            }
    
            public void clickOnViewReport() throws Exception {
                Thread.sleep(7000);
                WebDriverWait wait=new WebDriverWait(driver,20);
                WebElement clickOnViewReportButton=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(PageUtility.CLICK_ON_VIEWREPORT_BUTTON)));
                clickOnViewReportButton.click();
            }
    
    
            public void clearSelections() {
                clearSelections.click();
    
            }
    
            public void clickOnHomeIcon() {
                homeIcon.click();
    
            }
    
            public void scrollUp(){
                ((JavascriptExecutor) driver).executeScript("scroll(0, -250);");
            }
    
        }  
    

    TestCase Class:

            package com.deere.gmsr.testcase; 
            import org.openqa.selenium.support.PageFactory;
            import org.testng.Assert;
            import org.testng.annotations.BeforeClass;
            import org.testng.annotations.Test;
            import com.deere.gmsr.business.MhCurrentMonthReportBusiness;
            import com.deere.gmsr.driver.Driver;
    
        public class MhCurrentMonthReportTest {
    
            public MhCurrentMonthReportBusiness mhCurrentMonthReportBusiness;
    
    
            public MhCurrentMonthReportTest() throws Exception {
    
            mhCurrentMonthReportBusiness = PageFactory.initElements(Driver.getDriver(), MhCurrentMonthReportBusiness.class);
    
            }
    
    
    
            @Test(priority=8)
            public void CurrentMonthReport() throws Exception{      
                mhCurrentMonthReportBusiness.clickToCreateNewButton();
                mhCurrentMonthReportBusiness.selectEquipmentDivision();
                mhCurrentMonthReportBusiness.openLocation();
                mhCurrentMonthReportBusiness.selectGeography();
                mhCurrentMonthReportBusiness.selectMarketSegment();
                mhCurrentMonthReportBusiness.openProductWindow();
                mhCurrentMonthReportBusiness.selectHistoryReports();
                mhCurrentMonthReportBusiness.selectTimePeriod();
                mhCurrentMonthReportBusiness.scrollUp();
    
                mhCurrentMonthReportBusiness.clickOnViewReport();
                Thread.sleep(3000);
                mhCurrentMonthReportBusiness.clickOnHomeIcon();
        //      Driver.getDriver().navigate().back();
        //      mhCurrentMonthReportBusiness.clearSelections();
                Assert.assertTrue(true);
    
            }
    
        }  
    

    Loginbusiness Class:

        package com.deere.gmsr.business;
        import java.util.Properties;
    
        import org.openqa.selenium.WebDriver;
    
        import com.deere.gmsr.page.LoginPage;
        import com.deere.gmsr.utility.Constant;
        import com.deere.gmsr.utility.ScreenshotUtility;
        import com.deere.gmsr.utility.Utility;
    
        public class LoginBusiness  {
    
            private WebDriver driver;
            LoginPage loginPage;
            public LoginBusiness(WebDriver driver) throws Exception {
                this.driver = driver;
                loginPage = new LoginPage(driver);
    
            }
    
        //  public LoginDTO getLoginCredentials() throws Exception {
        //      Properties properties = Utility.getproperties(Constant.CONFIGURATION_PROPERTIES_FILE_PATH);
        //      String userName = properties.getProperty(Constant.USERNAME);
        //      String password = properties.getProperty(Constant.PASSWORD);
        //      LoginDTO loginDTO = new LoginDTO();
        //      loginDTO.setUserName(userName);
        //      loginDTO.setPassword(password);
        //      return loginDTO;
        //  }
    
            public void loginToApplication() throws Exception {
                loginPage.getUserNameTextField().clear();
                Properties properties = Utility.getproperties(Constant.CONFIGURATION_PROPERTIES_FILE_PATH);
                String username = properties.getProperty(Constant.USERNAME);
                loginPage.getUserNameTextField().sendKeys(username);
                String password = properties.getProperty(Constant.PASSWORD);
                loginPage.getPasswordTextField().clear();
            //  ScreenshotUtility.captureScreenShot(driver);
                loginPage.getPasswordTextField().sendKeys(password);
    
                loginPage.getSignInButton().click();
    
            }
    
        }  
    

    Login testclass:

        public class LoginTest{
    
            public LoginBusiness loginBusiness;
            public LoginTest() throws Exception {
                loginBusiness = new LoginBusiness(Driver.getDriver());
            }
    
        @Test
            public void loginToGmsrApplication() throws Exception {
    
        loginBusiness.loginToApplication();
    
    
    
            }  
    

    Driver class

    package com.deere.gmsr.driver;
    
    import java.util.Properties;
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.testng.annotations.AfterSuite;
    
    import com.deere.gmsr.utility.Constant;
    import com.deere.gmsr.utility.Message;
    import com.deere.gmsr.utility.Utility;
    
    import io.github.bonigarcia.wdm.WebDriverManager;
    
    public class Driver {
    
        private static WebDriver driver;
        private Driver() {
    
        }
    
        /**
         * 
         * @return
         * @throws Exception
         */
        public static synchronized WebDriver getDriver() throws Exception {
    
            if (null == driver) {
                Properties properties = Utility.getproperties(Constant.CONFIGURATION_PROPERTIES_FILE_PATH);
                String browser = properties.getProperty(Constant.BROWSER);
                String url = properties.getProperty(Constant.URL);
                switch (browser) {
                case Constant.CHROME:
                    WebDriverManager.chromedriver().setup();
                //  System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
                //  WebDriverManager.chromedriver().version(Constant.CHROME_DRIVER_VERSION).setup();
                    driver = new ChromeDriver();
                    // System.out.println(WebDriverManager.chromedriver().getVersions());
                    break;
                case Constant.IE:
                    WebDriverManager.iedriver().setup();
                    // WebDriverManager.iedriver().version(Constant.IE_DRIVER_VERSION).setup();
                    driver = new InternetExplorerDriver();
                    break;
                case Constant.FIREFOX:
                    WebDriverManager.firefoxdriver().setup();
                    // WebDriverManager.firefoxdriver().version(Constant.FIREFOX_DRIVER_VERSION).setup();
                    driver = new FirefoxDriver();
                    break;
                default:
                    System.out.println(Message.SUITABLE_DRIVER_NOT_FOUND);
                    break;
    
                }
                if (driver != null) {
                    driver.manage().window().maximize();
                    driver.manage().deleteAllCookies();
    
                    driver.get(url);
                    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                }
    
    
    
            }
    
            return driver;
    
        }
    
        public void navigateBack(){
            driver.navigate().back();
        }
    
        @AfterSuite
        public void quitDriver() {
            if (null != driver) {
    
                driver.quit();
            }
        }
    
    }
    

    • List item


    How to use this in our code so that we can successfully call login method for individual test cases as well as a suite.

    Thanks in advance!!



  • May this help you, just replace your test case class like this:-

        public class MhCurrentMonthReportTest extends LoginTest {
    
                public MhCurrentMonthReportBusiness mhCurrentMonthReportBusiness;
    
    
                public MhCurrentMonthReportTest() throws Exception {
    
                mhCurrentMonthReportBusiness = PageFactory.initElements(driver, MhCurrentMonthReportBusiness.class);
    
                }
    
    
    
                @Test(priority=2)
                public void CurrentMonthReport() throws Exception{ 
    
                 //Your test steps
                 }
    }   
    

    and your LoginTest Class like this:-

      public class LoginTest{
        WebDriver driver;
              @BeforeClass
              public void launchApplication()
                {
                   driver =Driver.getDriver();
                 }
    
            @Test(priority=1)
                public void loginToGmsrApplication() throws Exception {
        public LoginBusiness loginBusiness= new LoginBusiness(driver);
            loginBusiness.loginToApplication();
    
                }  
    

    In this once you execute your test class then before your test class method's only once your login method will execute and after that all test method will execute which are in ReportTest class.

    Now create XML file like this and run this:-

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="TestSuite" parallel="classes" thread-count="4">
    <test name="Test 1">
    <classes>
    <class name="com.deere.gmsr.testcase.MhCurrentMonthReportTest">
    </class>
    </classes>
    </test> 
    </suite>
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2