2016-02-12 89 views
0

當前設置, 我目前有我的測試設置,每個類實例化自己的瀏覽器。每個課程都基於我想測試的頁面。我現在想讓我的測試全部在一個瀏覽器中運行。目前已經建立了Mu代碼。我有一個通用方法被繼承的基本頁面,然後我有一個java.class用於特定頁面的方法,後面跟着一個test.class來運行我的測試。在一個瀏覽器中使用testng運行java selenium webdriver跨多個類進行測試

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); 

    } 

這將運行我的測試在不同的瀏覽器

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"); 

    } 

這將運行我的測試在一個瀏覽器

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"); 

    } 

} 

這將在一個瀏覽器中運行我的測試。然而,一個測試類中的所有對象都是不實際的。我想要做的是運行我的第一個測試類並實例化該瀏覽器,然後使用TestNG調用每個測試類並在一個瀏覽器中運行我的測試。但是,這樣做只會在包含@BeforeClass的第一個類中運行我的測試,這些測試運行並且下一個測試類被調用時,我會得到一個null pointer error。任何人都可以幫助我在所有測試類中的一個瀏覽器中運行我的測試。

+0

Hey..i建議運行在不同的browsers..that方式不同的測試你將不會鬆動並行測試運行..今天你可能有20個測試明天200 ..時間來運行他們會更多..我總是記住你將如何在設計框架並行運行你的測試 –

+0

我知道但此刻我需要在瀏覽器中運行所有測試。我可能會在稍後改變它,但現在這不是一個選項。 – trevdro

回答

0

看起來你是在每個測試的BeforeClass中啓動驅動程序並關閉之後。要僅在一個瀏覽器中工作,並在全部或全部套件執行後退出,只需創建一個類並在BeforeSuite中啓動驅動程序。將其擴展到你所有的測試類。例如

public class config{ 

static WebDriver driver; 

@BeforeSuite 
public void setup(){ 

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

} 

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

} 

將此類擴展爲您所有測試類。在你的測試類不再次使用該beforeclass設置和使用不關閉瀏覽器課餘

public class NewTest1 extends config{ 

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

謝謝你, 穆拉利

相關問題