简体   繁体   中英

Java/Selenium (ieDriver) - how to avoid repeating the webapp login Before Method (jUnit) in each Test run

I have Selenium IE driver, coding in Java. This Webapp is only compatible with IE9. I have to use SSL trust certificate and then Autoit_script to Bypass these Cert popups in IE9. I would like to find a solution where I do not have to repeat this code with each new Testcase/class. The IE9 gets so slow that the first Testcase does not finish executing, the second browser instance opens.

Is there a way to have this code placed in a way so I do not have to repeat it with each test? Here is the Code, just Logon to Web application:

public class LoginACMSbutton {
private static WebDriver driver;
private String baseUrl;
    @Before
public void setUp ()throws Exception {
    DesiredCapabilities ieCapabilities =         DesiredCapabilities.internetExplorer();
    driver = new InternetExplorerDriver(ieCapabilities);
    System.setProperty("webdriver.ie.driver","C:\\IEDriverServer.exe");
    baseUrl =("https://My website Link/");
    }

@Test
public void LogintoWeb() throws Exception { 
    driver.get(baseUrl);
    Runtime.getRuntime().exec("C:\\Autoitscripts\\IElogin.exe");

        try {
          WebDriverWait wait = new WebDriverWait(driver, 10);
                wait.until(ExpectedConditions.alertIsPresent());
                Alert alert = driver.switchTo().alert();
                alert.accept();
                    } catch (Exception e) {
                        System.out.println("show error");
                    }       //handle the popup
           }
 @After
public void End() throws Exception {
    driver.close();

}
}

At this time, I am just adding new Test info in @Test section, which is not smart way to do. :)

Please advise. Thanks in advance!

I'd suggest you to have a separate singleton class "Browser" or "Driver" to manage and handle the browser. you can call this class either in your page object or your tests(which would be hard to maintain). Another suggestion is to have a page object your login page that would have a method to log you in the system, you can call that in the test (create as many different logins as you need). Generally is not a good idea to mock-up login by storing a session or some quick and dirty hack, that's why I won't recommend it. Here's some info link . Hope it helps.

I have created PageObjects Package. Then Created Java class and a method name "DriverLogin", in this method I have added client certificate info, site URL with IE capabilities etc. Then I have called this method in Main Java class/PageObjects.

I have created Junit Framwork and extends each Junit Test class with PageObjects java Method. I dont have to copy and paste the same code over and over again. ppphhheewwww!

It works for me like a charm. No issues. Thank you all for your support and feedback. !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM