我是一个具有应用程序的selenium配置的基类。我想将类A(selenium configuration.class)用于另一个类B(Action.class),它又必须扩展C类(UIElemnts.class)。 我尝试使用Google guice绑定这样的类。
在这个例子中,我想在B类中使用setup方法和驱动程序对象,但是B类已经扩展到了Class。我只是想尝试使用Google Guice绑定类。
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Proxy.ProxyType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.BeforeTest;
import test.com.x.software.b.base.SeleniumConfiguration;
import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.Singleton;
import com.thoughtworks.selenium.SeleneseTestBase;
public class SeleniumConfiguration extends SeleneseTestBase{
public static WebDriver driver;
@BeforeTest
public static void setup() {
// Invoking firefox browser
FirefoxProfile firefoxobj = new FirefoxProfile();
firefoxobj.setPreference("network.proxy.type",
ProxyType.AUTODETECT.ordinal());
// System.out.println("********************");
driver = new FirefoxDriver(firefoxobj);
// System.out.println("********************"+Url);
driver.navigate().to("https://software.x.com");
// System.out.println("********************");
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
}
}
public class class B extends Class C{
@ Test
public static void createDeveloper() throws InterruptedException
{
// String currentdate=dateFormatting();
// String firstname="Test_Fn_"+currentdate;
// String lastname ="Test_Ln_"+currentdate;
// String loginid="Test_Tp_"+currentdate;
}
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class Class C extends PageFactory {
}
答案 0 :(得分:1)
我不完全清楚你要解决的问题是什么。如果你有B类并希望在B类中使用两个不同类(A和C)的方法,那么看起来你只需要在类A和C上声明类B的依赖关系,然后将公共方法委托给基础类A和C或在类B内部使用方法。这是一个SSCE:
public class SO24278992 {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
// Different concrete instances of A and C
// could be used here.
//
// Alternatively, you don't need these explicit
// bindings at all if the default contstructor
// does everything you need and you are not
// requiring explicit bindings.
bind(A.class).toInstance(new A());
bind(C.class).toInstance(new C());
}
});
B b = injector.getInstance(B.class);
b.useA();
b.useC();
}
static class B {
private A classA;
private C classC;
@Inject
public B(A a, C c) {
this.classA = a;
this.classC = c;
}
public void useC() {
classC.doSomething();
}
public void useA() {
classA.doSomethingElse();
}
}
static class A {
public void doSomethingElse() {
System.out.println("Doing something else in A");
}
}
static class C {
public void doSomething() {
System.out.println("Doing something in C");
}
}
}
我不确定这是否就是你所说的。
答案 1 :(得分:1)
我试图实现类似下面的内容。不确定它们是如何实现的。 (http://testng.org/doc/documentation-main.html)
5.18.2 - Guice dependency injection
If you use Guice, TestNG gives you an easy way to inject your test objects with a Guice module:
@Guice(modules = GuiceExampleModule.class)
public class GuiceTest extends SimpleBaseTest {
@Inject
ISingleton m_singleton;
@Test
public void singletonShouldWork() {
m_singleton.doSomething();
}
}
In this example, GuiceExampleModule is expected to bind the interface ISingleton to some concrete class:
public class GuiceExampleModule implements Module {
@Override
public void configure(Binder binder) {
binder.bind(ISingleton.class).to(ExampleSingleton.class).in(Singleton.class);
}
}
参与:从testng.org复制