繁体   English   中英

如何跨包使用同一对象

[英]How to use same object across packages

我有2个程序包,第1个基本程序package ceccms.automation.framework ,另一个程序package ceccms.testcases如下:package ceccms.automation.framework;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class UniversalMethods {

public static WebDriver driver = null;
public static String chromepath = "D:\\Automation\\Web Drivers\\ChromeDriver\\chromedriver.exe";
public static String edgepath = "D:\\Automation\\Web Drivers\\EdgeDriver\\MicrosoftWebDriver.exe";

public WebDriver openBrowser (String browser, String url) {

    if (browser != null) {
        switch (browser) {
        case "Mozilla":
            driver = new FirefoxDriver();
            driver.get(url);
            break;
        case "Chrome":
            System.setProperty("webdriver.chrome.driver", chromepath);
            driver = new ChromeDriver();
            driver.get(url);
            break;
        case "Edge":
            System.setProperty("webdriver.edge.driver", edgepath);
            driver = new EdgeDriver();
            driver.get(url);
            break;
        default:
            System.out.println("Wrong Browser Name");
            driver.quit();

        }
    }
    driver.manage().window().maximize();
    return driver;
}

}

package ceccms.testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import ceccms.automation.framework.UniversalMethods;

public class LoginTest {

public static String url = "https://test.ceccms.com/Login.aspx?";

static UniversalMethods U = new UniversalMethods();

public static void main(String[] args) throws InterruptedException {

    String browserName = "Chrome";
    U.openBrowser(browserName, url);

    WebElement userName = driver.findElement(By.id("txtUserName"));
    userName.sendKeys("pkumar");
    WebElement password = driver.findElement(By.id("txtUserPass"));
    password.sendKeys("PassMe33");
    Thread.sleep(3000);
    driver.quit();

}

}

我正在通过第二个程序包运行代码。 我想在整个包中使用一个对象driver ,而不使用符号U.driver.findElement() 我该如何实现?

解决方案之一是,您可以添加Page Objects,在其中定义Web元素,并将Driver作为参数来启动所有Web元素的Page对象类。 这将允许您直接使用元素。

您可以通过以下结构使用它(通过扩展通用方法):

package ceccms.testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import ceccms.automation.framework.UniversalMethods;

public class LoginTest extends UniversalMethods {

    //You can access driver and method without using object reference 

    public static String url = "https://test.ceccms.com/Login.aspx?";   

    public static void main(String[] args) throws InterruptedException {

    String browserName = "Chrome";
    openBrowser(browserName, url);

    WebElement userName = driver.findElement(By.id("txtUserName"));
    userName.sendKeys("pkumar");
    WebElement password = driver.findElement(By.id("txtUserPass"));
    password.sendKeys("PassMe33");
    Thread.sleep(3000);
    driver.quit();
    }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM