簡體   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