browserfunctions.java
public class BrowserFunctions {
BrowserLauncher bl = new BrowserLauncher();
WebDriver driver;
StringBuilder sb = new StringBuilder();
@BeforeSuite
public void initialioseBrowser() {
driver = bl.launchBrowser("Firefox");
}
@Parameters({ "URL" })
@BeforeSuite
public void invokeURL(String URL) {
driver.get(URL);
}
@AfterSuite
public void closeBrowser() {
driver.close();
}
login.java
public class Login {
BrowserLauncher bl = new BrowserLauncher();
WebDriver driver;
StringBuilder sb = new StringBuilder();
@Parameters({ "email", "password" })
@Test
public void logInTest(String email, String passowrd) {
Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKsignInCTA), "Sign in CTA visible");
CommonFunctions.clickButton(driver, PageElements.amzUKsignInCTA);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKEmailField), "Email field visible");
CommonFunctions.inputToField(driver, PageElements.amzUKEmailField, email);
Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKPasswordField),
"Password field visible");
CommonFunctions.inputToField(driver, PageElements.amzUKPasswordField, passowrd);
Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKSignInButton),
"Sign in button visible");
CommonFunctions.clickButton(driver, PageElements.amzUKSignInButton);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Assert.assertEquals(driver.getCurrentUrl(), "https://www.amazon.co.uk/?ref_=nav_ya_signin&");
}
java
public class Search {
BrowserLauncher bl = new BrowserLauncher();
WebDriver driver;
StringBuilder sb = new StringBuilder();
@Parameters({ "searchTerm" })
@Test
public void searchTest(String searchTerm) {
Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKSearchField),
"Search field visible");
CommonFunctions.inputToField(driver, PageElements.amzUKSearchField, searchTerm);
Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKSearchButton),
"Search button visible");
CommonFunctions.clickButton(driver, PageElements.amzUKSearchButton);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKResultContainer),
"Results container visible");
if (driver.findElements(PageElements.amzUKResultContainer).size() > 0) {
List<WebElement> resultContainerList = driver.findElements(PageElements.amzUKResultContainer);
for (WebElement w : resultContainerList) {
if (w.findElements(PageElements.amzUKResultTitle).size() > 0) {
if (w.findElement(PageElements.amzUKResultTitle).getText().contains(searchTerm)) {
} else {
sb.append(w.findElement(PageElements.amzUKResultTitle).getText() + " does not contain"
+ searchTerm + "\n");
}
}
Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKResultImage),
"Result image visible");
if (w.findElements(PageElements.amzUKResultImage).size() > 0) {
} else {
sb.append(w.findElement(PageElements.amzUKResultTitle).getText()
+ " does not contain an image\n");
}
Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKResultPrice),
"Result price visible");
if (w.findElements(PageElements.amzUKResultPrice).size() > 0) {
if (w.findElement(PageElements.amzUKResultPrice).getText().contains("£")) {
} else {
sb.append(w.findElement(PageElements.amzUKResultTitle).getText()
+ " does not contain a price\n");
}
}
}
}
System.out.println("searchTest(" + searchTerm + ") Failures:\n" + sb.toString());
}
BrowserLauncher bl = new BrowserLauncher();
WebDriver driver;
StringBuilder sb = new StringBuilder();
@Test
public void filterTest() {
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List<String> filterOptionsList = new ArrayList<String>();
sb.append(CommonFunctions.clickButton(driver, PageElements.amzUKFilterSeeMore));
Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKFilterOptions),
"Filter options visible");
for (int i = 0; i < driver.findElements(PageElements.amzUKFilterOptions).size(); i++) {
filterOptionsList.add(driver.findElements(PageElements.amzUKFilterOptions).get(i).getText());
}
for (String s : filterOptionsList) {
Assert.assertTrue(CommonFunctions.checkVisibility(driver, By.partialLinkText(s)), ""
+ s + " link visible");
if (driver.findElements(By.partialLinkText(s)).size() > 0) {
driver.findElement(By.partialLinkText(s)).click();
}else {
sb.append("Link " + s + " not visible");
continue;
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKFilterAnyCategory),
"\"Any Category\" button visible");
CommonFunctions.clickButton(driver, PageElements.amzUKFilterAnyCategory);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
sb.append(CommonFunctions.clickButton(driver, PageElements.amzUKFilterSeeMore));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
System.out.println("filterTest Failures:\n" + sb.toString());
}
public WebDriver launchBrowser(String BrowserName) {
if(BrowserName.equalsIgnoreCase("Firefox")) {
driver = new FirefoxDriver();
driver.manage().window().maximize();
} else if(BrowserName.equalsIgnoreCase("IE")) {
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "");
System.setProperty("webdriver.ie.driver", "C:\\Eclipse EE x86 Workspace\\ResidentAdvisorLabels\\IEDriverServer.exe");
driver = new InternetExplorerDriver(capabilities);
driver.get("javascript:document.getElementById('overridelink').click();");
} else if(BrowserName.equalsIgnoreCase("Chrome")) {
ChromeOptions op = new ChromeOptions();
op.addArguments("--user-data-dir=C:\\Users\\Bernard\\Desktop\\Selenium Data");
op.addArguments("--start-maximized");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
System.setProperty("webdriver.chrome.driver", "C:\\Eclipse EE x86 Workspace\\ResidentAdvisorLabels\\chromedriver.exe");
driver = new ChromeDriver(op);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
} else if(BrowserName.equalsIgnoreCase("PhantomJS")) {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("takesScreenshot", false);
String d = "\\";
capabilities.setCapability("phantomjs.binary.path", "C:"+d+"Eclipse EE x86 Workspace"+d+"phantomjs-2.0.0-windows"+d+"bin"+d+"phantomjs.exe");
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability("load-images", false);
driver = new PhantomJSDriver(capabilities);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
return driver;
}
<suite name="Suite1" verbose="1">
<parameter name="searchTerm" value="Selenium Webdriver" />
<parameter name="URL" value="Http://www.amazon.co.uk" />
<parameter name="email" value="scrubbed" />
<parameter name="password" value="scrubbed" />
<test name="AmazonUKTesting">
<classes>
<class name="tests.amazonUKTests.BrowserFunctions" />
<class name="tests.amazonUKTests.Login" />
<class name="tests.amazonUKTests.Search" />
<class name="tests.amazonUKTests.Filter" />
</classes>
</test>
您可以按照其他人的建议扩展基类,也可以使用TestNG Guice依赖项注入。例如:
TestModule.Class
import com.google.inject.Binder;
import com.google.inject.Module;
public class TestModule implements Module {
@Override
public void configure(Binder binder) {
BrowserLauncher bl = new BrowserLauncher();
WebDriver driver = bl.launchBrowser("Firefox");
binder.bind(WebDriver.class).toInstance(driver);
}
}
browserfunctions.java
import com.google.inject.Inject;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Guice;
import org.testng.annotations.Parameters;
@Guice(modules = {TestModule.class})
public class BrowserFunctions {
@Inject
WebDriver driver;
@Parameters({ "URL" })
@BeforeSuite
public void invokeURL(String URL) {
driver.get(URL);
}
@AfterSuite
public void closeBrowser() {
driver.close();
}
}
import com.google.inject.Inject;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
@Guice(modules = {TestModule.class})
public class Login {
@Inject
WebDriver driver;
StringBuilder sb = new StringBuilder();
@Parameters({ "email", "password" })
@Test
public void logInTest(String email, String passowrd) {
Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKsignInCTA), "Sign in CTA visible");
CommonFunctions.clickButton(driver, PageElements.amzUKsignInCTA);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKEmailField), "Email field visible");
CommonFunctions.inputToField(driver, PageElements.amzUKEmailField, email);
Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKPasswordField),
"Password field visible");
CommonFunctions.inputToField(driver, PageElements.amzUKPasswordField, passowrd);
Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKSignInButton),
"Sign in button visible");
CommonFunctions.clickButton(driver, PageElements.amzUKSignInButton);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Assert.assertEquals(driver.getCurrentUrl(), "https://www.amazon.co.uk/?ref_=nav_ya_signin&");
}
}
我们一直在试图找出如何从groovy脚本调用,(0),(1)selenium网站登录,然后调用另一个测试用例中的导航(脚本)部分。两种方法都能100%独立运行。下面是我用来从(0)调用这两个脚本的代码,但一旦登录完成。运行,将使用选择打开一个新浏览器。运行,测试显然会失败,因为它没有登录。 因此,我去操作了登录、选择(通过删除webdriver代码),主脚本代码现在看起来是这样的(我已经在粘贴中排
我得到了这个错误: “变量'a'已赋值,但其值永远不可用”
我有一个JPanel表单,其中包含一个JList和一些JButton。JPanel看起来像这样 当我单击Add List按钮时,会显示一个单独的JFrame表单。JFrame表单将如下所示 单击JFrame上的add按钮时,我需要将JTextfield(命名列表名)的值添加到上一个JPanel上的JList。我想知道如何将值从JFrame传递到JPanel?如有任何建议,将不胜感激。 下面是 JP
我是selenium初学者,只是想把webDriver传递给另一个类,但没有成功。程序应该打开浏览器(“Google”)并在主类中键入一些单词,在第二个类中应该按下“Google”按钮。 以下是我的主要课程: 这是我想从主驱动程序中使用的类。 当我运行它时,它对我说: 传递:main失败:printOnScreen java.lang.NullPointerException 我做错了什么?谢谢高
驱动程序类 社会阶层
可以在多个类之间传递数据吗? 我试图实现的是“一个类设置某个类的值,另一个类访问它”(java初学者)