0

驱动程序实例初始化为 Threadsafe。直到浏览器启动并传递 URL,一切似乎都在工作。但是凭据在任何人的浏览器上传递,而不是在所有实例上传递。

下面是设置和获取驱动程序的代码。

public static ThreadLocal<WebDriver> tldriver = new ThreadLocal<WebDriver>();

/**
 * This method is used to initialize on the basis of browser name
 * 
 * @param browserName
 * @return
 */

public WebDriver init_driver(String browserName) {
    if (browserName.equals("chrome")) {
        WebDriverManager.chromedriver().setup();
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
        chromeOptions.addArguments("--incognito");
        tldriver.set(new ChromeDriver(chromeOptions));

    } else if (browserName.equals("ff")) {
        WebDriverManager.firefoxdriver();
        tldriver.set(new FirefoxDriver());
    } else {
        System.out.println(browserName + "Browser value wrong, provide correct browser");
    }
    getDriver().manage().deleteAllCookies();
    getDriver().manage().window().maximize();
    return getDriver();
}

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

下面的代码我在我的测试类中使用

@BeforeMethod
public void setUp() {
    
    basePage = new BasePage();
    prop = basePage.init_proprties();
    String browser = prop.getProperty("browser");
    driver = basePage.init_driver(browser);
    loginPage = new LoginPage(driver);
    driver.get(prop.getProperty("url"));
    homePage = loginPage.doLogin(prop.getProperty("username"), prop.getProperty("password"));

    
}

测试套件----------------

<listeners>
    <listener
        class-name="com.qa.zoho.listeners.ExtentReportListener" />
    <!-- <listener class-name="com.qa.zoho.listeners.TestAllureListener" /> -->
</listeners>

<test name="CRM Sanity Test">
    <classes>
        <class name="com.qa.zoho.tests.HomePageTest" />


    </classes>
</test>
4

0 回答 0