1

我尝试使用 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 行业接受,因为我是测试自动化的新手。而且,如果它不在可接受的范围内,如果您能给我指导以提高我的知识,我将不胜感激。

任何与代码构造/代码质量/参数命名/测试用例编号等相关的建议/建议将不胜感激。

4

3 回答 3

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 
于 2019-08-21T08:33:23.767 回答
1

关于共享代码的一些注意事项,

  1. 我看到您复制了相同的代码。Ex driver.findElement(By.xpath("//input[@id='username']")).click(); 有3个重复。复制是不好的,因为它会使代码难以维护和阅读。
  2. 在单个 @Test 中测试 3 个测试场景最好将其划分为 3 个测试或使测试用例数据驱动
  3. 尝试模块化你的代码,这样可以增加可重用性。例如,@BeforeMethod 内容可以移动到单独的 DriverManager 类并使其可用于所有测试
  4. 尝试适应页面对象模型、页面工厂等设计模式
于 2019-08-21T09:07:14.363 回答
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");

这被称为 DRY 原则(不要重复自己)。进一步阅读这里这里

于 2019-08-21T12:53:56.150 回答