如何在java中使用TestNG Selenium webdriver在多个类中使用相同的浏览器窗口?

时间:2018-03-09 12:28:59

标签: java selenium selenium-webdriver testng selenium-chromedriver

我正在尝试自动化具有登录和登录后的网页有很多菜单项的网页。我想以这样的方式自动化它,它只登录网页一次,然后使用不同的菜单项。每个新菜单项自动化都在不同的类中创建。

Starting deploy EmulatorName ...  
Starting emulator EmulatorName ...  
C:\Users\USERNAME\AppData\Local\Android\Sdk\tools\emulator.EXE -partition-size 512 -no-boot-anim -avd EmulatorName -prop monodroid.avdname=EmulatorName  
emulator: WARNING: Crash service did not start  
emulator: WARNING: encryption is off  
Hax is enabled  
Hax ram_size 0x40000000  
HAX is working and emulator runs in fast virt mode.  
Emulator EmulatorName is running.  
Waiting for emulator to be ready...  
Deploying EmulatorName ...  
Build started.  
...  
Using "AndroidZipAlign" task from assembly "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MS  
  PackagedResources:  
    obj\Debug\android\bin\packaged_resources  
  SDCardDirectory:   
  External: False  
  ManifestTemplate: obj\Debug\android\AndroidManifest.xml  
  EnableInstantRun: False  
  InstantRunForceUploadApk: True  
  AndroidSdkPath: C:\Users\username\AppData\Local\Android\Sdk  
  AndroidNdkPath: C:\ProgramData\Microsoft\AndroidNDK64\android-ndk-r13b  
  JavaSdkPath: C:\Program Files (x86)\Java\jdk1.8.0_112  
  Found AndroidSdk at C:\Users\username\AppData\Local\Android\Sdk  
  Found AndroidNdk at C:\ProgramData\Microsoft\AndroidNDK64\android-ndk-r13b  
  Found AndroidTools at C:\Users\username\AppData\Local\Android\Sdk\tools  
DEBUG RunShellCommand emulator-5554 getprop  
TASK: GetProperties getprop completed?:[dalvik.vm.dex2oat-Xms]: [64m]  
[dalvik.vm.dex2oat-Xmx]: [512m]  
[dalvik.vm.heapsize]: [256m]  
[dalv...  

A类

package pack1;

public class Init {

    public WebDriver driver;

    ChromeOptions options;

    @BeforeSuite
    public void beforeSuite() throws AWTException, InterruptedException, IOException {
        //Setting Chrome Driver and disabling the save password option
        System.setProperty(“webdriver.chrome.driver”,”C:\\Users\\user\\Desktop\\Demo\\chromedriver.exe”);
        options = new ChromeOptions();
        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put(“credentials_enable_service”, false);
        prefs.put(“profile.password_manager_enabled”, false);
        options.setExperimentalOption(“prefs”, prefs);
        driver=new ChromeDriver(options);

        //Opening the URL
        driver.get(“myURL”);
        driver.manage().window().maximize();

        //Login to the portal
        driver.findElement(By.xpath(“.//*[@id=’content-wrapper’]/div/div/div/div/div/div/div/form/div/div[1]/input”)).sendKeys(username);
        driver.findElement(By.xpath(“.//*[@id=’content-wrapper’]/div/div/div/div/div/div/div/form/div/div[2]/input”)).sendKeys(password);
        driver.findElement(By.xpath(“.//*[@id=’content-wrapper’]/div/div/div/div/div/div/div/form/div/div[3]/button”)).click();

    }

    @AfterSuite
    public void afterSuite() {
        //Closing the driver
        // driver.close();
    }
}

B类

    package pack1;

public class ClassA extends Init{

    @Test (priority=0, enabled = true)
    public void Setup() throws InterruptedException{
        //Traversing the menu to reach contract grower setup
        Thread.sleep(5000);
        driver.findElement(By.linkText(“Menu1”)).click();
        driver.findElement(By.linkText(“SubMenu1”)).click();

    }

}

testing.xml

    package pack1;

public class ClassBextends Init{

    @Test (priority=0, enabled = true)
    public void Setup() throws InterruptedException{
        //Traversing the menu to reach contract grower setup
        Thread.sleep(5000);
        driver.findElement(By.linkText(“Menu2”)).click();
        driver.findElement(By.linkText(“SubMenu2”)).click();
    }

}

1 个答案:

答案 0 :(得分:3)

您应该进行以下更改:

  • WebDriver班级中的Init配置为static
  • 不要继承测试类中的Init
  • 要在测试类中使用driver,请将其作为Init.getDriver();
  • 进行访问

基类

public class Init {

    private static WebDriver driver;

    public static WebDriver getDriver() {
        return driver;
    }

    @BeforeSuite
    public void beforeSuite() {
        System.out.println("BS");
        System.setProperty("webdriver.chrome.driver", "");
        driver = new ChromeDriver();
        driver.get("https://www.google.com");
    }

    @AfterSuite
    public void afterSuite() {
        System.out.println("AS");
        driver.quit();
    }
}

A类

public class ClassA {
    @Test(priority = 0, enabled = true)
    public void classATest() throws InterruptedException {
        System.out.println("classATest");
        Init.getDriver().findElement(By.name("q")).sendKeys("Class 1");
    }
}

B类

public class ClassB {
    @Test(priority = 0, enabled = true)
    public void class2Test() throws InterruptedException {
        System.out.println("classBTest");
        Init.getDriver().findElement(By.name("q")).sendKeys("Class 2");
    }
}

TestNG XML文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="System Testing" parallel="none" thread-count="1">
    <test name="MenuTest" verbose="0">
        <classes>
            <class name="com.pack1.ClassA" />
            <class name="com.pack1.ClassB" />
            <class name="com.pack1.Init" />
        </classes>
    </test>
</suite> 

<强>输出

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
BS
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 16311
Only local connections are allowed.
Mar 09, 2018 2:33:59 PM org.openqa.selenium.remote.ProtocolHandshake.createSession
INFO: Detected dialect: OSS
classATest
classBTest
AS
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.541 sec - in TestSuite