我有2个软件包,第1个基本软件包package ceccms.automation.framework
和另一个软件包package ceccms.testcases
如下:
包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()
。我该如何实现?
答案 0 :(得分:0)
其中一种解决方案是,您可以添加Page Objects,在其中定义Web元素,并为您的Driver提供参数以启动所有Web元素的Page对象类。这将使您可以直接使用元素。
答案 1 :(得分:0)
您可以通过以下结构使用它(通过扩展通用方法):
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();
}
}