请在以下情况下帮助我:1.我有一个基类,并且有2.two页登录页和主页3.two测试页,我的问题是,从第一个测试页我必须将驱动程序详细信息传递给第二个测试页,但是没有发生。 //封装Pom;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import Baseclass.Baseclass;
import Pages.Homepage;
import Pages.Loginpage;
public class Loginpom extends Baseclass {
public WebDriver driver;
public Loginpage lp;
public Homepage hp;
@BeforeTest
public void getd() {
driver=getdriver();
}
@Test
public void lpom() throws InterruptedException {
lp=new Loginpage(driver);
lp.login();
hp= lp.transfer();
}
}
//主页软件包Pom;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import Baseclass.Baseclass;
import Pages.Homepage;
import Pages.Loginpage;
public class Homepom {
public WebDriver driver;
public Homepage hp;
public Loginpage lp;
@Test
public void hpom() {
System.out.println(driver);
hp.home();
}
}
答案 0 :(得分:0)
您可以利用Singleton Pattern的使用,无需传递对象,而仅在每个地方创建和使用单个WebDriver。我已经为此写了一个代码段,请检查是否有帮助。
private BasePage()
{
s = "I am singleTon Constructor ";
}
public static WebDriver getInstance()
{
if (driver == null)
driver = new WebDriver("Chrome");
return driver;
}
}
class WebDriver
{
private String name ;
public WebDriver(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
class LoginPage
{
WebDriver d ;
public void OpenLoginPage()
{
System.out.println("Opening LoginPage");
}
public void showDriverName() {
System.out.println( d.getName());
}
public WebDriver getDriver() {
WebDriver d1 = BasePage.getInstance();
this.d =d1;
return d ;
}
}
class HomePage
{
WebDriver d ;
public WebDriver getDriver() {
WebDriver d = new WebDriver("Chrome");
this.d = d;
return d;
}
public void showDriverName() {
System.out.println(d.getName());
}
}
public class BaseClass {
public static void main(String[] args) {
HomePage p = new HomePage();
WebDriver d = p.getDriver();
p.showDriverName();
LoginPage l = new LoginPage();
WebDriver d2= l.getDriver();
l.showDriverName();
}
}
答案 1 :(得分:0)
您可以将驱动程序详细信息从一个类传递到另一类,如下所示:
类Loginpom:
@Test
public void lpom() throws InterruptedException {
lp=new Loginpage(driver);
lp.login();
hp= lp.transfer();
}
普通Homepom:
public Homepom(WebDriver driver)
{
Homepom.driver = driver;
OR
this.driver = driver ;
}
必须在类中为您调用驱动程序的驱动程序分配为Parameterized构造函数。因此,在您的代码中,必须为Homepom类分配带有Driver对象的参数化构造函数。