2017-03-08 60 views
1

我正在一臺機器上運行ThreadLocal測試。我在@BeforeMethod中使用代碼來啓動網頁。我也嘗試將它作爲我的Base Test類中的方法單獨編寫,並在@BeforeMethod中調用它。並行TestNG測試與ExtentReports使用錯誤的@BeforeMethod

一切似乎工作正常,除了錯誤的用戶正在用錯誤的測試登錄。這很奇怪,因爲Login方法不在BaseTest類中,它是從頁面對象中調用的(並且它工作正常,除了登錄錯誤的用戶進行測試)。

在這個例子中,我已經將問題代碼包含在@BeforeMethod中,並且還註釋了單獨的「初始化」方法和方法調用,所以您可以看到我嘗試過的兩種方法。

public class StackExample { 

protected String baseURL; 
public String browser; 
private static ThreadLocal<WebDriver> threadedDriver = new ThreadLocal<WebDriver>(); 

@BeforeMethod(alwaysRun = true) 
@Parameters({ "browser", "loginType" }) 
public void setup(String browser, String loginType, Method caller) 
     throws MalformedURLException, InterruptedException { 
    WebDriver driver = null; 

    // Browsers 
    if (browser.equalsIgnoreCase("Internet Explorer")) { 
     System.setProperty("webdriver.ie.driver", "C:\\Users\\automation\\Selenium\\IEDriverServer.exe"); 
     driver = new InternetExplorerDriver(); 
    } else if (browser.equalsIgnoreCase("Firefox")) { 
     System.setProperty("webdriver.gecko.driver", "C:\\Users\\automation\\Selenium\\geckodriver.exe"); 
     ProfilesIni firProfiles = new ProfilesIni(); 
     FirefoxProfile wbdrverprofile = firProfiles.getProfile("Webdriver2"); 
     driver = new FirefoxDriver(wbdrverprofile); 
    } else if (browser.equalsIgnoreCase("chrome")) { 
     System.setProperty("webdriver.chrome.driver", "C:\\Users\\automation\\Selenium\\chromedriver.exe"); 
     driver = new ChromeDriver(); 
    } else if (browser.equalsIgnoreCase("MicrosoftEdge")) { 
     System.setProperty("webdriver.edge.driver", "C:\\Users\\automation\\Selenium\\MicrosoftWebDriver.exe"); 
     driver = new EdgeDriver(); 
    } 

    setWebDriver(driver); 
    this.browser = browser; 
    System.out.println(browser); 
    // initialize(loginType); 
    System.out.println(loginType); 

    if (loginType.equalsIgnoreCase("client")) 
     ClientReportFactory 
       .getTest(StringUtils.join("Client Test"), ' ') + " (" 
         + browser + ")", "This test is located in class: " + getClass().getName()); 
    else if (loginType.equalsIgnoreCase("advisor")) 
     AdvisorReportFactory 
       .getTest(StringUtils.join("Advisor Test"), ' ') + " (" 
         + browser + ")", "This test is located in class: " + getClass().getName()); 

    if (loginType.equalsIgnoreCase("client")) 
     baseURL = "ClientWebsite.example"; 
    else if (loginType.equalsIgnoreCase("advisor")) 
     baseURL = "AdvisorWebsite.example"; 
    else { 
     System.out.println("Client or Advisor must be specified in TestNG XML"); 
    } 
    driver.get(baseURL); 
    driver.manage().window().maximize(); 

} 

// public void initialize(String loginType) throws InterruptedException { 
// if (loginType.equalsIgnoreCase("client")) 
// baseURL = "ClientWebsite.example"; 
// else if (loginType.equalsIgnoreCase("advisor")) 
// baseURL = "AdvisorWebsite.example"; 
// else{ 
// System.out.println("Client or Advisor must be specified in TestNG XML"); 
// } 
// driver.get(baseURL); 
// driver.manage().window().maximize(); 
// 
// } 

public static WebDriver getDriver() { 
    return threadedDriver.get(); 
} 

static void setWebDriver(WebDriver driver) { 
    threadedDriver.set(driver); 
} 

@AfterMethod // (alwaysRun = true) 
@Parameters({ "loginType" }) 
public void afterMethod(Method caller, String loginType) { 
    // Here we are making sure we close the same test we opened. 
    System.out.println(loginType); 
    if (loginType.equalsIgnoreCase("client")) 
     ClientReportFactory 
       .closeTest(StringUtils.join("Client Test"), ' ') + " (" 
         + browser + ")"); 
    else if (loginType.equalsIgnoreCase("advisor")) 
     AdvisorReportFactory 
       .closeTest(StringUtils.join("Advisor Test"), ' ') + " (" 
         + browser + ")"); 
    getDriver().quit(); 
    threadedDriver.set(null); 
} 

@AfterSuite 
@Parameters({ "loginType" }) 
public void afterSuite(String loginType) { 

    if (loginType.equalsIgnoreCase("client")) 
     ClientReportFactory.closeReport(); 
    else if (loginType.equalsIgnoreCase("advisor")) 
     AdvisorReportFactory.closeReport(); 

    if (getDriver() != null) { 
     getDriver().quit(); 
    } else { 
     System.out.println("Drivers already closed"); 
    } 
} 
} 

這裏是我的測試方法。他們從頁面對象中調用實際的SignIn方法。我知道這不是問題,因爲從頁面對象調用的所有其他方法在正確的測試中工作正常。

public class StackExampleTests extends StackExample { 

@Test(enabled = true, priority = 0) 
public void ClientTest1() throws Exception { 
    ExtentTest t = ClientReportFactory.getTest(); 
    t.log(LogStatus.INFO, "Client 1 Login for " + browser); 
    try { 
     Login objLogin = new Login(getDriver()); 
     String username = "username1"; 
     String password = "password1"; 
     t.log(LogStatus.INFO, "Logging in as user: " + username); 
     objLogin.SignIn(username, password); 

     // perform First Client's tests here 

    } catch (Exception e) { 
     t.log(LogStatus.WARNING, "Exception found: " + e.getMessage().substring(0, 400) 
       + t.addBase64ScreenShot(ClientReportFactory.CaptureScreen(getDriver()))); 
    } 
} 

@Test(enabled = true, priority = 1) 
public void ClientTest2 throws Exception{ 
    ExtentTest t = ClientReportFactory.getTest(); 
    t.log(LogStatus.INFO, "Client 2 Login for " + browser); 
    try { 
     Login objLogin = new Login(getDriver()); 
     String username = "username2"; 
     String password = "password2"; 
     t.log(LogStatus.INFO, "Logging in as user: " + username); 
     objLogin.SignIn(username, password); 

     // perform Second Client's tests here 

    } catch (Exception e) { 
     t.log(LogStatus.WARNING, "Exception found: " + e.getMessage().substring(0, 400) 
       + t.addBase64ScreenShot(ClientReportFactory.CaptureScreen(getDriver()))); 
    } 
} 
} 

我認爲這個跟蹤意味着它將在同一個線程中啓動我的所有3個測試。但我不能完全

Starting ChromeDriver 2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9) on port 44694 
Only local connections are allowed. 

Starting ChromeDriver 2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9) on port 12513 
Only local connections are allowed. 

Starting ChromeDriver 2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9) on port 32651 
Only local connections are allowed. 
+0

當您嘗試在測試完成後運行此測試時,是否還剩下一大堆額外的瀏覽器?快速瀏覽讓我相信靜態驅動程序是一個問題,但我不記得'testNG'如何將所有內容放在一起 – mrfreester

+0

不是,就像現在一樣,測試成功完成並關閉。只是登錄錯誤。對於某些測試,登錄無關緊要,例如只檢查標題鏈接。這些都通過罰款。但似乎登錄會隨機轉換,因此對於具有特定數據的帳戶,測試失敗。可能是因爲所調用的任何方法都被隨機分配到一個測試。 – dsidler

+0

另一個奇怪的是我修改了Selenium Grid上的一個Threadlocal 設置的代碼。隨着RemoteWebDriver一切運行良好,這從來沒有發生過。只有在本地機器上纔會感到困惑。 – dsidler

回答

0

它看起來像你的測試都具有用WebDriver並行運行的本地時屬於自己的司機身份危機。

設置ThreadLocal<WebDriver> threadedDrivergetDriver()setDriver()static應該解決這個問題。