Recommendation about the code construction

只愿长相守 提交于 2019-12-11 06:08:05

问题


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();
   } 
 }

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.


回答1:


Few things to note regarding the shared code,

  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



回答2:


since you are working with Katalon, it has inbuild function to store all the objects(object repository ) ex: "//input[@id='username']", https://docs.katalon.com/katalon-studio/docs/manage-test-object.html

you can move below into that place

    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 



回答3:


To give you an example of what @Asanka is talking about when mentioning code duplication, take a look at this. You often have this piece of code repeating

driver.findElement(By.xpath("//input[@id='username']")).click();
driver.findElement(By.xpath("//input[@id='username']")).clear();
driver.findElement(By.xpath("//input[@id='username']")).sendKeys("");

on different elements.

A better approach would be to put all three lines in a method. Let's call that method 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("");
}

Next, you might notice that the Xpath of the element is also repeated three times. So, let's extract the XPath as a parameter:

public static void sendKeysToElement(String xpathToElement){
    driver.findElement(By.xpath(xpathToElement)).click();
    driver.findElement(By.xpath(xpathToElement)).clear();
    driver.findElement(By.xpath(xpathToElement)).sendKeys("");
}

That would simplify your test to (I'm only going to show you this up to the //Empty password validation part):

    //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");

This is known as the DRY principle (Don't Repeat Yourself). Further reading here and here.



来源:https://stackoverflow.com/questions/57584908/recommendation-about-the-code-construction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!