有关代码构造的建议 [英] Recommendation about the code construction

查看:89
本文介绍了有关代码构造的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Katalon Studio和Selenium | testNG自动登录.我已经使用XML文件将浏览器值发送到该脚本,并将其粘贴到此处.

I have tried to automate the login with Katalon Studio and Selenium|testNG. I have used XML file to send the browser value to the script, which I have pasted here.

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

I just wanted to know whether my code construction will be accepted or not by the QA industry as I am new to test automation. And also if it is not in the accepted range, I would really appreciate if you can give me guidance to improve my knowledge.

任何与代码构造/代码质量/参数命名/测试用例编号等有关的建议/建议都将受到高度赞赏.

Any advices/suggestions related to the code construction/code quality/parameter naming/testcase numbering etc. would be highly appreciated.

推荐答案

关于共享代码的几点注意事项,

Few things to note regarding the shared code,

  1. 我看到您重复了相同的代码.前任 driver.findElement(By.xpath("//input [@ id ='username']")).click(); 有3个重复项. 复制是不好的,因为它会使代码难以维护和阅读.
  2. 在单个@Test中测试3个测试场景 最好将其划分为3个测试或制作测试用例数据驱动程序
  3. 尝试对您的代码进行模块化,这样可以提高可重用性.例如,可以将@BeforeMethod内容移动到单独的DriverManager类,并使其在所有测试中都可用
  4. 尝试适应诸如
  1. I see you have duplicated the same code. Ex driver.findElement(By.xpath("//input[@id='username']")).click(); Has 3 duplicates. Duplicating is bad since it will make code difficult to maintain and read.
  2. Testing 3 test scenarios within single @Test Better to divide this to 3 tests or make test case data drivern
  3. Try to modular your code, so can increase the reusability. For example @BeforeMethod content can be moved to separate DriverManager class and make it useable on all the the tests
  4. Try to adapt a design pattern like Page object Model, Page Factory

这篇关于有关代码构造的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆