简体   繁体   English

使用testng在一个浏览器中跨多个类运行Java Selenium Webdriver测试

[英]Running java selenium webdriver tests across many classes in one browser using testng

Current set up, I currently have my tests set up that each class instantiates its own browser. 当前设置,我目前已设置测试,每个类实例化其自己的浏览器。 Each class is based on a page I want to test. 每个课程都基于我要测试的页面。 I now want my tests to all to run in the one browser. 现在,我希望所有测试都可以在一个浏览器中运行。 Mu code has the current set up. Mu代码具有当前设置。 I have a base page where common methods are inherited, I then have a java.class for the methods of a specific page followed by a test.class to run my tests from. 我有一个继承通用方法的基本页面,然后有一个java.class用于特定页面的方法,后跟一个test.class来运行我的测试。

public class BasePage {

@FindBy(id = "j_username") WebElement field_Username;
@FindBy(id = "j_password") WebElement field_Password;
@FindBy(name = "login") WebElement button_Login;

public WebDriver driver;
public WebDriverWait wait;

protected JavascriptExecutor jsExecutor;

public String PAGE_URL;
public String PAGE_TITLE;

public BasePage(WebDriver driver){
    this.driver = driver;
    jsExecutor = ((JavascriptExecutor) driver);
    wait = new WebDriverWait(driver, 200);
}

public void loadPage(){

    driver.get(getPageUrl());
    driver.manage().window().maximize();
    for(int i=0; i<2; i++){ 
        driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT));
        } 
}

protected void login(String username, String password){
    findDynamicElement(By.id("j_username") , 7);
    setText_Login(username);
    setText_PasswordLogin(password);
    clickLoginMain();
}

public class CreateBillingRange  extends BasePage {

    @FindBy(id = "j_username") WebElement field_Username;
    @FindBy(id = "j_password") WebElement field_Password;
    @FindBy(name = "login") WebElement button_Login;
    @FindBy(id = "add_label") WebElement button_AddLabel;
    @FindBy(linkText = "Policy") WebElement field_Policy;
    @FindBy(linkText = "SPCM") WebElement field_SPCM;
    @FindBy(linkText = "Billing Range") WebElement field_BR;
    @FindBy(id = "addChargingProfile_label") WebElement button_addChargingProfile;
    @FindBy(name = "addBillingNumberRange") WebElement button_addBillingNumberRange;
    @FindBy(id = "startRange") WebElement field_StartRange; 
    @FindBy(id = "endRange") WebElement field_EndRange; 
    @FindBy(id = "name") WebElement field_Name;
    @FindBy(name = "saveBillingNumberRange") WebElement button_SaveBillingNumberRange;

    @FindBy(id = "number") WebElement field_NumberSearch;
    @FindBy(name = "search") WebElement field_Submit;
    @FindBy(name = "ids") WebElement checkbox_Number;
    @FindBy(name = "deleteBillingNumberRange") WebElement button_Delete;
    @FindBy(id = "okDialogButton_label") WebElement okDialogButton_label;



    public CreateBillingRange(WebDriver driver) {
        super(driver);
        this.PAGE_URL = "http://xxxxx:xxx/page";
    }

    public void login(String username, String password){
        findDynamicElement(By.id("j_username") , 7);
        setText_Login(username);
        setText_PasswordLogin(password);
        clickLoginMain();
    }

    public void setText_Login(String text){
        setElementText(field_Username, text);
    }


    public void setText_PasswordLogin(String text){
        setElementText(field_Password, text);
    }

    public void clickLoginMain(){
        clickElement(button_Login);
    }

    public void click_SignUpButton(){
        clickElement(button_Login);
    }

    public void openCharging() {

        findDynamicElement(By.linkText("Policy") , 19);
        clickElement(field_Policy);
        clickElement(field_SPCM);
        clickElement(field_BR);

    }   

    public void addBillingRange(String start, String end){
        findDynamicElement(By.name("addBillingNumberRange"), 10);
        clickElement(button_addBillingNumberRange);
        findDynamicElement(By.id("startRange"), 3);
        setElementText(field_StartRange, start);
        setElementText(field_EndRange, end);
        clickElement(button_SaveBillingNumberRange);
        findDynamicElement(By.id("successMessages") , 5);
    }

    public void deleteBillingRange(String BillingRange){
        findDynamicElement(By.name("search"),20);
        field_NumberSearch.click();
        field_NumberSearch.sendKeys(BillingRange);
        field_Submit.click();
        findDynamicElement(By.name("ids"),20);
        checkbox_Number.click();
        button_Delete.click();
        accpetConfirmSuccess();

    }




public class CreateBundle  extends BasePage{

    @FindBy(id = "j_username") WebElement field_Username;
    @FindBy(id = "j_password") WebElement field_Password;
    @FindBy(name = "login") WebElement button_Login;
    @FindBy(id = "add_label") WebElement button_AddLabel;
    @FindBy(linkText = "Policy") WebElement field_Policy;
    @FindBy(linkText = "SPCM") WebElement field_SPCM;
    @FindBy(linkText = "Plan Mgmt") WebElement field_PlanMgmt;
    @FindBy(linkText = "Bundles") WebElement field_Bundles;
    @FindBy(id = "addOcsButton_label") WebElement button_addOCS;
    @FindBy(id = "alias") WebElement field_alias;
    @FindBy(id = "value") WebElement field_value;
    @FindBy(id = "submitActionButton_label") WebElement button_OCSsave; 
    @FindBy(id = "add") WebElement button_addLabel;
    @FindBy(id = "dataPlanName") WebElement field_DataPlan;
    @FindBy(id = "addComboPackServiceButton_label") WebElement button_addComboService;
    @FindBy(id = "amount") WebElement field_amount;
    @FindBy(id = "ocsAccountReference") WebElement field_OCSAccount;
    @FindBy(id = "days") WebElement field_Days;
    @FindBy(id = "submitActionButton_label") WebElement button_SubmitAction;
    @FindBy(id = "save_label") WebElement button_BundleSave;

    @FindBy(id = "dataPlanName") WebElement field_OCSSearch;
    @FindBy(id = "search_label") WebElement field_OCSSubmit;
    @FindBy(name = "ids") WebElement checkbox_OCS;
    @FindBy(id = "delete_label") WebElement button_DeleteOCS;
    @FindBy(xpath = "/html/body/div/div[2]/div[1]/form/fieldset[3]/div[2]/table/tbody/tr[4]/td[1]/input") WebElement field_OCSDelete;
    @FindBy(id = "deleteOcsRef_label") WebElement button_deleteOCS;


    public CreateBundle(WebDriver driver) {
        super(driver);
        this.PAGE_URL = "http://xxxxx.xxx.x/page";
    }


    public void openBundle() {

        findDynamicElement(By.linkText("Policy") , 19);
        clickElement(field_Policy);
        clickElement(field_SPCM);
        clickElement(field_PlanMgmt);
        clickElement(field_Bundles);

    }   

This will run my tests in separate browsers 这将在单独的浏览器中运行我的测试

public class BillingRange_Test {


    public WebDriver driver;
    public WebDriverWait wait;
    CreateBillingRange CreateBillingRange;


    @BeforeClass(alwaysRun = true)
    public void setup() throws MalformedURLException{
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setBrowserName("firefox");
        this.driver = new RemoteWebDriver(new URL("http://192.168.1.237:5556/wd/hub"), capabilities);
        wait = new WebDriverWait(driver, 10);
        CreateBillingRange = PageFactory.initElements(driver, CreateBillingRange.class );
    }

    @AfterClass(alwaysRun = true)
    public void teardown(){
        this.driver.quit();
    }


    @BeforeTest
    public void testSignUpMainPage(){
        CreateBillingRange.loadPage();
        CreateBillingRange.login("super","5tg7uj6yh");

    }

    @Test(priority=1,retryAnalyzer=Retry.class)
    public void addBillingRange(){
        CreateBillingRange.openCharging();
        CreateBillingRange.addBillingRange("8800088","1111111111");

    }






public class Bundle_Test {


    public WebDriver driver;
    public WebDriverWait wait;
    CreateBundle CreateBundle;


    @BeforeClass(alwaysRun = true)
    public void setup() throws MalformedURLException{
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setBrowserName("firefox");
        this.driver = new RemoteWebDriver(new URL("http://192.168.1.237:5556/wd/hub"), capabilities);
        wait = new WebDriverWait(driver, 10);
        CreateBundle = PageFactory.initElements(driver, CreateBundle.class );
    }

    @AfterClass(alwaysRun = true)
    public void teardown(){
        this.driver.quit();
    }


    @Test(priority=1)
    public void testSignUpMainPage(){
        CreateBundle.loadPage();
        CreateBundle.login("super","5tg7uj6yh");

    }

    @Test
    public void addBundle(){
        CreateBundle.openBundle();
        CreateBundle.createBundles("Test", "1","AFL","1");

    }

This will run my tests in the one browser 这将在一个浏览器中运行我的测试

    public WebDriver driver;
    public WebDriverWait wait;
    protected String baseUrl;
    protected String nodeURL;
    CreateBillingRange CreateBillingRange;
    CreateBundle CreateBundle;


    @BeforeClass(alwaysRun = true)
    public void setup() throws MalformedURLException{
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setBrowserName("firefox");
        this.driver = new RemoteWebDriver(new URL("http://192.168.1.236:5555/wd/hub"), capabilities);
        wait = new WebDriverWait(driver, 10);
        CreateBillingRange = PageFactory.initElements(driver,CreateBillingRange.class );
        CreateBundle = PageFactory.initElements(driver, CreateBundle.class );
    }




    //@AfterClass(alwaysRun = true)
    public void teardown(){
        this.driver.quit();
    }


    @Test(priority=20)
    public void testSignUpMainPage(){
        CreateBillingRange.loadPage();
        CreateBillingRange.login("super","5tg7uj6yh");
        System.out.println("Yes");

    }

    @Test(priority=21)
    public void addOCSAccount(){
        CreateBundle.openBundle();
        CreateBundle.createOSCAccount("Name Selenium WebDriver", "2");

    }

}

This will run my test in one browser. 这将在一个浏览器中运行我的测试。 However it is not practical to have all by objects in the one test class. 但是,将所有对象都放在一个测试类中是不实际的。 What I wish to do is run my first test class and instantiate the broswer then using TestNG call each test class and run my tests in the one browser. 我想要做的是运行我的第一个测试类并实例化浏览器,然后使用TestNG调用每个测试类并在一个浏览器中运行我的测试。 However doing this will only run my tests in the first class which contains the @BeforeClass when these tests are ran and the next test class is called I get a null pointer error . 但是,这样做只会在运行了这些测试并且调用下一个测试类时在包含@BeforeClass的第一个类中运行我的测试,而我得到一个null pointer error Can anyone help me run my tests in the one browser across all my test classes. 谁能帮助我在所有测试类中的一个浏览器中运行测试。

It looks like you are initiating the driver in BeforeClass of each test and closing after. 看起来您正在每个测试的BeforeClass中启动驱动程序,然后在之后关闭。 To work only in one browser and to quit after all or total suite is executed, just create one class and initiate driver in BeforeSuite. 要仅在一个浏览器中工作并在全部或全部套件执行后退出,只需创建一个类并在BeforeSuite中启动驱动程序即可。 extend this to all you test classes. 扩展到所有测试类。 for example 例如

 public class config{

 static WebDriver driver;

 @BeforeSuite
 public void setup(){

driver=new FirefoxDriver(); //here use your setup

}

@AfterSuite
public void tearDown(){
driver.quit();
}

}

Extends this class to all you test class. 将此课程扩展到所有测试课程。 in your test classes dont use that setup in beforeclass again and dont use closing the browser afterclass 在测试类中,不要再使用beforeclass中的设置,也不要使用在类结束后关闭浏览器

  public class NewTest1 extends config{

   @Test
    public void test1() {
    driver.get("http://www.google.com");
   }
  }

Thank You, Murali 谢谢,穆拉利

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Selenium WebDriver,Selenium Grid和testNG运行并行测试 - Running Parallel Tests using Selenium WebDriver, Selenium Grid and testNG 如何在java中使用TestNG Selenium webdriver在多个类中使用相同的浏览器窗口? - How to use the same browser window in multiple classes using TestNG Selenium webdriver in java? 在同一个浏览器中运行所有测试 - Running all tests in the same browser webdriver testng Selenium - TestNG:测试未并行运行 - 使用 selenium + java + browserstack - Selenium - TestNG: Tests are not running in parallel - Using selenium + java + browserstack 使用webdriver-factory运行Selenium Webdriver TestNG测试 - Run Selenium Webdriver TestNG tests using webdriver-factory 在使用 Java 运行一些 Selenium WebDriver 测试之前清除缓存 - Clear cache before running some Selenium WebDriver tests using Java Selenium Webdriver,使用Java的testNG - Selenium webdriver , testNG with java Selenium WebDriver,带有Java的TestNG - Selenium WebDriver, TestNG with Java 使用 selenium webdriver 和 java testng 中的数据提供程序 - data providers in testng using selenium webdriver and java 使用Selenium webdriver + TestNG + Java在不同的浏览器上运行测试 - To run tests on different browsers using Selenium webdriver+ TestNG+Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM