下面是我尝试用来添加新用户的脚本 -
package pages;
import static org.testng.AssertJUnit.assertEquals;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.openqa.selenium.support.ui.Select;
import appSetup_Maven.test.BaseClass;
import appSetup_Maven.test.GenerateData;
public class AddCustomer extends BaseClass {
@BeforeMethod
public void before() {
new GenerateData();
}
@Test(priority = 0)
public void addCustomerPageNavigation() throws IOException, InterruptedException {
Login();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='sidebar-menu']/ul/li[3]/a/span[1]")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='sidebar-menu']/ul/li[3]/ul/li[1]/a")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(
By.xpath(".//*[@id='wrapper']/div[3]/div/div[2]/div/div/div/div[1]/div/div[1]/div/div/button")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(
By.xpath(".//*[@id='wrapper']/div[3]/div/div[2]/div/div/div/div[1]/div/div[1]/div/div/ul/li/a"))
.click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id='Organization_org_name']"))
.sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='Organization_org_trading_name']"))
.sendKeys(GenerateData.generateRandomString(20));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='Organization_org_number']"))
.sendKeys(GenerateData.generateRandomNumber(10));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='Organization_phone_number']"))
.sendKeys(GenerateData.generateRandomString(20));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//WebElement ListCountry = driver.findElement(By.xpath(".//*[@id='Organization_fk_country_codes_id']"));
//Select country = new Select(ListCountry);
//country.deselectByVisibleText("India");
//WebElement ListState = driver.findElement(By.xpath(".//*[@id='Organization_fk_country_codes_id']"));
//Select state = new Select(ListState);
//country.deselectByVisibleText("Assam");
driver.findElement(By.xpath(".//*[@id='Organization_address_street']']"))
.sendKeys(GenerateData.generateRandomString(20));
driver.findElement(By.xpath(".//*[@id='Organization_address_line2']"))
.sendKeys(GenerateData.generateRandomString(20));
driver.findElement(By.xpath(".//*[@id='Organization_address_suburb']"))
.sendKeys(GenerateData.generateRandomString(20));
driver.findElement(By.xpath(".//*[@id='Organization_address_postalcode']"))
.sendKeys(GenerateData.generateRandomString(20));
//WebElement NumEmp = driver.findElement(By.xpath(".//*[@id='Organization_numberOfEmployees']"));
//Select Emp = new Select(NumEmp);
//country.deselectByVisibleText("5-10");
//WebElement NumUsr = driver.findElement(By.xpath(".//*[@id='Organization_totalUserSize']"));
//Select Usr = new Select(NumUsr);
//country.deselectByVisibleText("5-10");
driver.findElement(By.xpath(".//*.//*[@id='Organization_domain_name']"))
.sendKeys(GenerateData.generateRandomString(20));
driver.findElement(By.xpath(".//*[@id='Users_first_name']")).sendKeys(GenerateData.generateRandomString(20));
driver.findElement(By.xpath(".//*[@id='Users_last_name']")).sendKeys(GenerateData.generateRandomString(20));
WebElement Role = driver.findElement(By.xpath(".//*[@id='Users_fk_business_role_id']"));
Select Usrrole = new Select(Role);
//country.deselectByVisibleText("Other");
driver.findElement(By.xpath(".//*[@id='Users_username']")).sendKeys(GenerateData.generateRandomString(20));
driver.findElement(By.xpath(".//*[@id='confirm-username']")).sendKeys(GenerateData.generateRandomString(20));
driver.findElement(By.xpath(".//*[@id='Users_password']"))
.sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.findElement(By.xpath(".//*[@id='re-password']")).sendKeys(GenerateData.generateRandomAlphaNumeric(30)); // fail
WebElement rListCountry = driver.findElement(By.xpath(".//*[@id='Organization_fk_country_codes_id']"));
Select ccountry = new Select(rListCountry);
//country.deselectByVisibleText("India");
WebElement TimeZone = driver.findElement(By.xpath(".//*[@id='Organization_fk_country_codes_id']"));
Select Time = new Select(TimeZone);
//country.deselectByVisibleText("Asia/Kolkata");
driver.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_first_name']"))
.sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_last_name']"))
.sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_email']"))
.sendKeys(GenerateData.generateEmail(10));
driver.findElement(By.xpath(".//*[@id='users-form']/div/div/div[5]/div/h2/div/ins"))
.sendKeys(GenerateData.generateEmail(10));
WebElement dListCountry = driver
.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_fk_country_codes_id']"));
Select acountry = new Select(dListCountry);
//country.deselectByVisibleText("India");
WebElement aListState = driver
.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_fk_country_subdivisions_id']"));
Select astate = new Select(aListState);
//country.deselectByVisibleText("Assam");
driver.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_street']"))
.sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_address_line2']"))
.sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_city']"))
.sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_postal_code']"))
.sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.findElement(By.xpath(".//*[@id='users-form']/div/div/div[7]/div[2]/div[2]/input[1]")).click();
assertEquals("Success", driver.getTitle());
}
}
以下是我用于生成随机数据的类 -
package appSetup_Maven.test;
import org.apache.commons.lang3.RandomStringUtils;
public class GenerateData {
public static String generateRandomString(int length) {
return RandomStringUtils.randomAlphabetic(length);
}
public static String generateRandomNumber(int length) {
return RandomStringUtils.randomNumeric(length);
}
public static String generateRandomAlphaNumeric(int length) {
return RandomStringUtils.randomAlphanumeric(length);
}
public static String generateStringWithAllobedSplChars(int length, String allowdSplChrs) {
String allowedChars = "abcdefghijklmnopqrstuvwxyz" + // alphabets
"1234567890" + // numbers
allowdSplChrs;
return RandomStringUtils.random(length, allowedChars);
}
public static String generateEmail(int length) {
String allowedChars = "abcdefghijklmnopqrstuvwxyz" + // alphabets
"1234567890" + // numbers
"_-."; // special characters
String email = "";
String temp = RandomStringUtils.random(length, allowedChars);
email = temp.substring(0, temp.length() - 9) + "@test.org";
return email;
}
public String generateUrl(int length) {
String allowedChars = "abcdefghijklmnopqrstuvwxyz" + // alphabets
"1234567890" + // numbers
"_-."; // special characters
String url = "";
String temp = RandomStringUtils.random(length, allowedChars);
url = temp.substring(0, 3) + "." + temp.substring(4, temp.length() - 4) + "."
+ temp.substring(temp.length() - 3);
return url;
}
}
以下是添加客户页面导航代码。请求您审查延迟道歉 -
package pages;
import static org.testng.AssertJUnit.assertEquals;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.openqa.selenium.support.ui.Select;
import appSetup_Maven.test.BaseClass;
import appSetup_Maven.test.GenerateData;
public class AddCustomer extends BaseClass {
@BeforeMethod
public void before() {
new GenerateData();
}
@Test(priority = 0)
public void addCustomerPageNavigation() throws IOException, InterruptedException {
Login();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='sidebar-menu']/ul/li[3]/a/span[1]")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='sidebar-menu']/ul/li[3]/ul/li[1]/a")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(
By.xpath(".//*[@id='wrapper']/div[3]/div/div[2]/div/div/div/div[1]/div/div[1]/div/div/button")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(
By.xpath(".//*[@id='wrapper']/div[3]/div/div[2]/div/div/div/div[1]/div/div[1]/div/div/ul/li/a"))
.click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id='Organization_org_name']"))
.sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='Organization_org_trading_name']"))
.sendKeys(GenerateData.generateRandomString(20));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='Organization_org_number']"))
.sendKeys(GenerateData.generateRandomNumber(10));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='Organization_phone_number']"))
.sendKeys(GenerateData.generateRandomString(20));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//WebElement ListCountry = driver.findElement(By.xpath(".//*[@id='Organization_fk_country_codes_id']"));
//Select country = new Select(ListCountry);
//country.deselectByVisibleText("India");
//WebElement ListState = driver.findElement(By.xpath(".//*[@id='Organization_fk_country_codes_id']"));
//Select state = new Select(ListState);
//country.deselectByVisibleText("Assam");
driver.findElement(By.xpath(".//*[@id='Organization_address_street']']"))
.sendKeys(GenerateData.generateRandomString(20));
driver.findElement(By.xpath(".//*[@id='Organization_address_line2']"))
.sendKeys(GenerateData.generateRandomString(20));
driver.findElement(By.xpath(".//*[@id='Organization_address_suburb']"))
.sendKeys(GenerateData.generateRandomString(20));
driver.findElement(By.xpath(".//*[@id='Organization_address_postalcode']"))
.sendKeys(GenerateData.generateRandomString(20));
//WebElement NumEmp = driver.findElement(By.xpath(".//*[@id='Organization_numberOfEmployees']"));
//Select Emp = new Select(NumEmp);
//country.deselectByVisibleText("5-10");
//WebElement NumUsr = driver.findElement(By.xpath(".//*[@id='Organization_totalUserSize']"));
//Select Usr = new Select(NumUsr);
//country.deselectByVisibleText("5-10");
driver.findElement(By.xpath(".//*.//*[@id='Organization_domain_name']"))
.sendKeys(GenerateData.generateRandomString(20));
driver.findElement(By.xpath(".//*[@id='Users_first_name']")).sendKeys(GenerateData.generateRandomString(20));
driver.findElement(By.xpath(".//*[@id='Users_last_name']")).sendKeys(GenerateData.generateRandomString(20));
WebElement Role = driver.findElement(By.xpath(".//*[@id='Users_fk_business_role_id']"));
Select Usrrole = new Select(Role);
//country.deselectByVisibleText("Other");
driver.findElement(By.xpath(".//*[@id='Users_username']")).sendKeys(GenerateData.generateRandomString(20));
driver.findElement(By.xpath(".//*[@id='confirm-username']")).sendKeys(GenerateData.generateRandomString(20));
driver.findElement(By.xpath(".//*[@id='Users_password']")).sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.findElement(By.xpath(".//*[@id='re-password']")).sendKeys(GenerateData.generateRandomAlphaNumeric(30)); // fail
WebElement rListCountry = driver.findElement(By.xpath(".//*[@id='Organization_fk_country_codes_id']"));
Select ccountry = new Select(rListCountry);
//country.deselectByVisibleText("India");
/*String email = genData.generateEmail(30);
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys(email);
driver.findElement(By.id("emailconfirm")).clear();
driver.findElement(By.id("emailconfirm")).sendKeys(email);
*/
WebElement TimeZone = driver.findElement(By.xpath(".//*[@id='Organization_fk_country_codes_id']"));
Select Time = new Select(TimeZone);
//country.deselectByVisibleText("Asia/Kolkata");
driver.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_first_name']"))
.sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_last_name']"))
.sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_email']"))
.sendKeys(GenerateData.generateEmail(10));
driver.findElement(By.xpath(".//*[@id='users-form']/div/div/div[5]/div/h2/div/ins"))
.sendKeys(GenerateData.generateEmail(10));
WebElement dListCountry = driver
.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_fk_country_codes_id']"));
Select acountry = new Select(dListCountry);
//country.deselectByVisibleText("India");
WebElement aListState = driver
.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_fk_country_subdivisions_id']"));
Select astate = new Select(aListState);
//country.deselectByVisibleText("Assam");
driver.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_street']"))
.sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_address_line2']"))
.sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_city']"))
.sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.findElement(By.xpath(".//*[@id='OrganizationBillingDetails_postal_code']"))
.sendKeys(GenerateData.generateRandomAlphaNumeric(30));
driver.findElement(By.xpath(".//*[@id='users-form']/div/div/div[7]/div[2]/div[2]/input[1]")).click();
assertEquals("Success", driver.getTitle());
}
}
以下是基类会要求您查看 -
package appSetup_Maven.test;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import java.io.FileInputStream;
import java.util.Properties;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class BaseClass {
protected static WebDriver driver;
protected static PropertiesGrabber properties = PropertiesGrabber.getInstance();
@BeforeSuite
public void Setup() throws IOException {
try {
properties.init("E:\\Git Repos\\n2s-sun-selenium-automation\\datadriven.properties");
initDriver();
maximize();
getDriver().get(properties.get("url"));
waitEle();
Reporter.log("===Browser Started==", true);
} catch (Exception ex) {
Reporter.log("Error::::" + ex.getMessage());
}
}
private void initDriver() {
if (null != driver) {
return;
}
if (properties.exists("browserName", "firefox")) {
driver = new FirefoxDriver();
return;
}
if (properties.exists("browserName", "chrome")) {
driver = new ChromeDriver();
return;
}
System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
public void FullScreenWait() {
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
protected WebDriver getDriver() {
return driver;
}
public void Login() throws IOException {
try {
getDriver().get(properties.get("url"));
getElement(".//*[@id='LoginForm_username']").sendKeys("avinash@abcd.com");
getElement(".//*[@id='LoginForm_password']").sendKeys("abcd");
getElement(".//*[@id='login-form']/button").click();
System.out.println("==Logged in==");
} catch (Exception ex) {
System.out.println("==may be Logged in== " + ex.getMessage());
}
}
protected WebElement getElement(String xPath) {
return driver.findElement(By.xpath(xPath));
}
public void GenData() {
GenerateData genData;
genData = new GenerateData();
}
protected void maximize() {
driver.manage().window().maximize();
}
protected void waitEle() {
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
@AfterTest
public void close() {
driver.quit();
}
@AfterSuite
public void Close() {
// driver.quit();
}
}
错误消息,我与标题相同,但下面是确切的堆栈跟踪 -
> Tests run: 15, Failures: 1, Errors: 0, Skipped: 0, Time elapsed:
> 115.588 sec <<< FAILURE! - in TestSuite
> addCustomerPageNavigation(pages.AddCustomer) Time elapsed: 4.641 sec <<< FAILURE!
> org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
> Build info: version: '2.45.0', revision: '5017cb8e7ca8e37638dc3091b2440b90a1d8686f', time: '2015-02-27
> 09:10:26'
> System info: host: 'DESKTOP-1AKPPH7', ip: '192.168.56.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version:
> '1.8.0_111'
> Driver info: driver.version: FirefoxDriver
> at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
> at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
> at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
> at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
> at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
> at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
> at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
> at java.net.Socket.connect(Socket.java:589)
> at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
> at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:125)
> at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:319)
> at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
> at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
> at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
> at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
> at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
> at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
> at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
> at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
> at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:126)
> at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:72)
> at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:133)
> at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.execute(NewProfileExtensionConnection.java:165)
> at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:362)
> at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572)
> at org.openqa.selenium.firefox.FirefoxDriver.access$100(FirefoxDriver.java:73)
> at org.openqa.selenium.firefox.FirefoxDriver$1$1.implicitlyWait(FirefoxDriver.java:219)
> at pages.AddCustomer.addCustomerPageNavigation(AddCustomer.java:28)
> Results :
>
> Failed tests:
> AddCustomer.addCustomerPageNavigation:28 » UnreachableBrowser Error communicat...
>
> Tests run: 15, Failures: 1, Errors: 0, Skipped: 0
Questions -<br/>
1. How do I fix the above error and why and in which case do we get this error.<br/>
2. There are issues in this script, such as the randomly generated e-mails don't match, How can I do fix this ?
答案 0 :(得分:1)
每个初始化的驱动程序应在测试完成后关闭退出。
driver.close将关闭驱动程序焦点所在的当前浏览器窗口。并且driver.quit将关闭此驱动程序打开的所有浏览器会话/窗口。
答案 1 :(得分:0)
您没有提供Login()的代码,但我猜测您有驱动程序初始化和
之间或其他位置的driver.close()或driver.quit()