我尝试使用Katalon Studio和Selenium | testNG自动登录。我已经使用XML文件将浏览器值发送到该脚本,并将其粘贴到此处。
public class TC_Testportal {
private WebDriver driver;
private String baseUrl;
@Parameters("browser")
@BeforeMethod
public void beforeMethod(String browser) {
if (browser.equals("firefox")) {
System.setProperty("webdriver.gecko.driver", "drivers\\geckodriver.exe");
driver = new FirefoxDriver();
baseUrl = "https://test.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} else if (browser.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
driver = new ChromeDriver();
baseUrl = "https://test.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
}
@Test
public void tc001() {
driver.get(baseUrl);
//Empty user-name|password validation
driver.findElement(By.xpath("//input[@id='username']")).click();
driver.findElement(By.xpath("//input[@id='username']")).clear();
driver.findElement(By.xpath("//input[@id='username']")).sendKeys("");
driver.findElement(By.xpath("//input[@id='userpassword']")).click();
driver.findElement(By.xpath("//input[@id='userpassword']")).clear();
driver.findElement(By.xpath("//input[@id='userpassword']")).sendKeys("");
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='SIGN IN'])[1]/following::div[2]")).click();
System.out.println("Empty user-name|password validation - CHECKED");
//Empty password validation
driver.findElement(By.xpath("//input[@id='username']")).click();
driver.findElement(By.xpath("//input[@id='username']")).clear();
driver.findElement(By.xpath("//input[@id='username']")).sendKeys("test");
driver.findElement(By.xpath("//input[@id='userpassword']")).click();
driver.findElement(By.xpath("//input[@id='userpassword']")).clear();
driver.findElement(By.xpath("//input[@id='userpassword']")).sendKeys("");
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='SIGN IN'])[1]/following::button[1]")).click();
System.out.println("Empty password validation - CHECKED");
//Empty user-name validation
driver.findElement(By.xpath("//input[@id='username']")).click();
driver.findElement(By.xpath("//input[@id='username']")).clear();
driver.findElement(By.xpath("//input[@id='username']")).sendKeys("");
driver.findElement(By.xpath("//input[@id='userpassword']")).click();
driver.findElement(By.xpath("//input[@id='userpassword']")).clear();
driver.findElement(By.xpath("//input[@id='userpassword']")).sendKeys("123");
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='SIGN IN'])[1]/following::div[2]")).click();
System.out.println("Empty user-name validation - CHECKED");
}
@AfterMethod
public void afterMethod() {
driver.quit();
}
}
我只是想知道我的代码构造是否会被QA行业接受,因为我是自动化测试的新手。而且,如果它不在可接受的范围内,那么如果您能给我一些指导以提高我的知识,我将不胜感激。
任何与代码构造/代码质量/参数命名/测试用例编号等有关的建议/建议都将受到赞赏。
答案 0 :(得分:1)
因为您使用的是Katalon,它具有内置功能来存储所有对象(对象存储库),例如:“ // input [@ id ='username']”,https://docs.katalon.com/katalon-studio/docs/manage-test-object.html
您可以在下面移动到该位置
driver.findElement(By.xpath("//input[@id='username']")).clear();
driver.findElement(By.xpath("//input[@id='username']")).sendKeys("");
driver.findElement(By.xpath("//input[@id='userpassword']")).click();
driver.findElement(By.xpath("//input[@id='userpassword']")).clear();
driver.findElement(By.xpath("//input[@id='userpassword']")).sendKeys("");
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='SIGN IN'])[1]/following::div[2]")).click();
driver.findElement(By.xpath("//input[@id='username']")).click();
driver.findElement(By.xpath("//input[@id='username']")).clear();
driver.findElement(By.xpath("//input[@id='username']")).sendKeys("test");
driver.findElement(By.xpath("//input[@id='userpassword']")).click();
driver.findElement(By.xpath("//input[@id='userpassword']")).clear();
driver.findElement(By.xpath("//input[@id='userpassword']")).sendKeys("");
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='SIGN IN'])[1]/following::button[1]")).click();
driver.findElement(By.xpath("//input[@id='username']")).click();
driver.findElement(By.xpath("//input[@id='username']")).clear();
driver.findElement(By.xpath("//input[@id='username']")).sendKeys("");
driver.findElement(By.xpath("//input[@id='userpassword']")).click();
driver.findElement(By.xpath("//input[@id='userpassword']")).clear();
driver.findElement(By.xpath("//input[@id='userpassword']")).sendKeys("123");
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='SIGN IN'])[1]/following::div[2]")).click();```
and if you are using Katalon you don't need to call the browser drivers, since Katalon has those in their product
答案 1 :(得分:1)
关于共享代码的几点注意事项,
答案 2 :(得分:1)
为您提供@Asanka在提及代码重复时正在谈论的内容的示例,请看一看。您经常会重复这段代码
driver.findElement(By.xpath("//input[@id='username']")).click();
driver.findElement(By.xpath("//input[@id='username']")).clear();
driver.findElement(By.xpath("//input[@id='username']")).sendKeys("");
在不同的元素上。
更好的方法是将所有三行都放在一个方法中。我们将该方法称为sendKeysToElement()
:
public static void sendKeysToElement(){
driver.findElement(By.xpath("//input[@id='username']")).click();
driver.findElement(By.xpath("//input[@id='username']")).clear();
driver.findElement(By.xpath("//input[@id='username']")).sendKeys("");
}
接下来,您可能会注意到元素的Xpath也被重复了3次。因此,让我们将XPath提取为参数:
public static void sendKeysToElement(String xpathToElement){
driver.findElement(By.xpath(xpathToElement)).click();
driver.findElement(By.xpath(xpathToElement)).clear();
driver.findElement(By.xpath(xpathToElement)).sendKeys("");
}
这将简化您的测试,(我仅向您介绍//Empty password validation
部分):
//Empty user-name|password validation
sendKeysToElement("//input[@id='username']")
sendKeysToElement("//input[@id='userpassword']")
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='SIGN IN'])[1]/following::div[2]")).click();
System.out.println("Empty user-name|password validation - CHECKED");