我對Selenium進行測試,根據用戶的輸入訪問網站。如何根據用戶的輸入在JFrame中同時啓動瀏覽器實例?
在JFrame上,用戶可以輸入網站的地址,然後按'運行'。這將調用Firefox瀏覽器實例並導航到URL。
但是,我希望能夠根據用戶的輸入同時啓動多個瀏覽器實例。
因此,當前測試仍在運行時,用戶可以通過JFrame輸入不同的URL鏈接,然後按'運行',這將啓動導航到輸入地址的另一個Firefox瀏覽器實例。
任何人都可以給我一個想法,我怎麼能做到這一點?
這是我到目前爲止。
public class TestFrame {
static JFrame frame;
static String url;
public static void frame() {
frame = new JFrame();
frame.setSize(350, 100);
frame.setLocationRelativeTo(null);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JTextField text = new JTextField(20);
JButton button = new JButton("Run");
panel.add(text);
panel.add(button);
frame.add(panel);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
url = text.getText();
Testng func = new Testng();
func.testRun();
}
});
frame.setVisible(true);
}
public static void main(String[] args) {
frame();
}
}
我有一個集線器和節點以編程方式設置,並有一個線程本地遠程網絡驅動程序。
public class TestConfig {
ThreadLocal<RemoteWebDriver> driver;
Hub hub;
SelfRegisteringRemote remote;
@BeforeClass
public void setUp() throws Exception {
// Start hub
GridHubConfiguration config = new GridHubConfiguration();
config.setHost("localhost");
config.setPort(4444);
hub = new Hub(config);
hub.start();
SelfRegisteringRemote node = null;
RegistrationRequest req = new RegistrationRequest();
// Create capabilities instance
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
capabilities.setCapability(RegistrationRequest.MAX_INSTANCES,5);
// Set configurations for registration request
Map<String, Object> nodeConfig = new HashMap<String, Object>();
nodeConfig.put(RegistrationRequest.AUTO_REGISTER, true);
nodeConfig.put(RegistrationRequest.HUB_HOST, hub.getHost());
nodeConfig.put(RegistrationRequest.HUB_PORT, hub.getPort());
nodeConfig.put(RegistrationRequest.PORT, 5555);
URL remoteURL = new URL("http://" + hub.getHost() + ":" + 5555);
nodeConfig.put(RegistrationRequest.PROXY_CLASS,
"org.openqa.grid.selenium.proxy.DefaultRemoteProxy");
nodeConfig.put(RegistrationRequest.MAX_SESSION, 5);
nodeConfig.put(RegistrationRequest.CLEAN_UP_CYCLE, 2000);
nodeConfig.put(RegistrationRequest.REMOTE_HOST, remoteURL);
nodeConfig.put(RegistrationRequest.MAX_INSTANCES, 5);
// Registration request
req.addDesiredCapability(capabilities);
req.setRole(GridRole.NODE);
req.setConfiguration(nodeConfig);
// Register node
node = new SelfRegisteringRemote(req);
node.startRemoteServer();
node.startRegistrationProcess();
}
@BeforeMethod
public void start() throws MalformedURLException, IOException, Exception {
driver = new ThreadLocal<RemoteWebDriver>();
// Set capabilities
DesiredCapabilities dc = new DesiredCapabilities();
FirefoxProfile profile = new FirefoxProfile();
dc.setCapability(FirefoxDriver.PROFILE, profile);
dc.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
URL remoteURL = new URL("http://" + hub.getHost() + ":" + hub.getPort()
+ "/wd/hub");
RemoteWebDriver remoteDriver = new RemoteWebDriver(remoteURL, dc);
driver.set(remoteDriver);
}
public WebDriver getDriver() {
return driver.get();
}
@AfterClass
public void shutdown() throws Exception {
if (remote != null) {
remote.stopRemoteServer();
System.out.println("Node stopped.");
}
if (hub != null) {
hub.stop();
System.out.println("Hub stopped.");
}
}
}
這是一個示例測試。我曾嘗試使用@DataProvider,但需要一個數組,而不是我正在尋找的。
public class TestRun extends TestConfig {
@Test
public void test() {
getDriver().get(url);
getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
getDriver().manage().window().maximize();
}
}
最後,這是一個編程編寫的testng類。
public class Testng {
public void testRun() {
XmlSuite suite = new XmlSuite();
suite.setName("Project");
XmlTest test = new XmlTest(suite);
test.setName("Downloader");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("example.testing.TestRun"));
test.setXmlClasses(classes);
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
}
}