如何在Selenium Java项目中将多个“如果不是”语句格式化为“公共静态无效值”方法

时间:2019-02-06 03:29:33

标签: java selenium cucumber

我有一个Java代码,在一个方法中包含许多IF Else语句。下面是示例一。 我如何将它们转换为单个方法,我需要将这些IF Else语句重构为Methods,并且必须在Cucumber-Selenium框架的StepDefinition中使用这些重构的方法。

有人可以帮我吗...

Supending

我必须为这些If Else语句创建一个方法- 加载 刷新页面 插入数据 上传文件 waitElVisibility waitElInVisibility

3 个答案:

答案 0 :(得分:0)

如果条件允许,您可以在类中为其他所有方法创建单独的方法,以便将来继续执行这些操作时,可以直接使用该方法并执行该操作。我现在正在使用私有访问修饰符,但是如果您也想在类外使用该方法,则可以将其公开。并且,如果不使用else if,则应该使用switch,以便将来有其他动作起作用时,只需在switch条件中添加它即可。

您的代码应类似于:

 public class ActionClass{

 // Making all methods for the actions you need to perform
 private static void loadData(){
    Reporter.log(description+"|"+data);
    driver.get(data);
    if(!TestBase.browserName.equals("Chrome"))
    {
    driver.manage().window().maximize();
    screenSize=driver.manage().window().getSize().toString();
    System.out.println("My screensize is "+screenSize);
    }       
}

private static void refreshPage(){
    driver.navigate().refresh();                         
    wait.until(ExpectedConditions.visibilityOfElementLocated(                        
    By.xpath("//span[contains(text(),'salesforce.com, inc. All rights 
    reserved.')]")));
}


private static void insertData(){
    Reporter.log(description+"|"+data);
    moveToElement(elementReference, referenceValue);
    findElement(elementReference, referenceValue).click();
    findElement(elementReference, referenceValue).clear();

    if (description.toLowerCase().contains("request name")||                         
    referenceValue.contains("reqNme")){
    String customNum=getDate("requestName");
    findElement(elementReference, referenceValue).sendKeys(data+customNum);
    System.out.println("Request Name is "+data+customNum);
    } else {
    findElement(elementReference, referenceValue).sendKeys(data);
    }
}


private static void uploadFile(){
    File file = new File(data);
    String filePath=file.getAbsolutePath();
    System.out.println(filePath);
    findElement(elementReference, referenceValue).clear();
    findElement(elementReference, referenceValue).sendKeys(filePath);
}

private static void uploadImageAction(){
    Reporter.log(description+"||"+data);
    uploadImage(elementReference, referenceValue, "Logo", new File(data));

}

private static void waitElVisibility(){
    wait = new WebDriverWait(driver,Long.parseLong(data));
    wait.until(ExpectedConditions.visibilityOf(findElement(elementReference, 
    referenceValue)));

}

private static void waitElInvisibility(){
    wait = new WebDriverWait(driver,Long.parseLong(data));
    wait.until(ExpectedConditions.invisibilityOfElementLocated(
    By.xpath(referenceValue)));

}

// The main method where switch condition will be present
    public static void main(String[] args) {
// Initialise the action string according to your code
    String action = null;
    switch (action) {
    case ("Load"):
        loadData();
        break;
    case ("RefreshPage"):
        refreshPage();
        break;

    case ("InsertData"):
        insertData();
        break;

    case ("uploadFile"):
        uploadFile();
        break;

    case ("uploadImage"):
        uploadImageAction();
        break;

    case ("waitElVisibility"):
        waitElVisibility();
        break;

    case ("waitElInVisibility"):
        waitElInvisibility();
        break;

    default:
        System.out.println("Action did not match");
    }
}

答案 1 :(得分:0)

您可以将if-else块内容拆分为多种方法,如果要摆脱if-else,那么可以使用switch语句。参见下面的代码:

public class PublicStaticVoids {

    private static WebDriver driver;
    private static WebDriverWait wait;

    private static void loadThePage(String description, String data) {
        Reporter.log(description+"|"+data);
        driver.get(data); // Initialize the driver before doing this
        if(!TestBase.browserName.equals("Chrome"))
        {
            driver.manage().window().maximize();
            String screenSize = driver.manage().window().getSize().toString();
            System.out.println("My screensize is "+screenSize);
        }
    }

    private static void refreshThePage(String description, String data, WebElement elementReference, String referenceValue) {
        Reporter.log(description+"|"+data);
        moveToElement(elementReference, referenceValue);
        findElement(elementReference, referenceValue).click();
        findElement(elementReference, referenceValue).clear();

        if (description.toLowerCase().contains("request name")||                         
                referenceValue.contains("reqNme")){
            String customNum = getDate("requestName");
            findElement(elementReference, referenceValue).sendKeys(data+customNum);
            System.out.println("Request Name is "+data+customNum);
        } else {
            findElement(elementReference, referenceValue).sendKeys(data);
        }
    }

    private static void uploadTheFile(String data, WebElement elementReference, String referenceValue) {
        File file = new File(data);
        String filePath=file.getAbsolutePath();
        System.out.println(filePath);
        findElement(elementReference, referenceValue).clear();
        findElement(elementReference, referenceValue).sendKeys(filePath);
    }

    private static void uploadTheImage(String description, String data, WebElement elementReference, String referenceValue) {
        Reporter.log(description+"||"+data);
        uploadImage(elementReference, referenceValue, "Logo", new File(data));
    }

    private static void waitForVisibility(String data, WebElement elementReference, String referenceValue) {
        wait = new WebDriverWait(driver,Long.parseLong(data));
        wait.until(ExpectedConditions.visibilityOf(findElement(elementReference, referenceValue)));
    }

    private static void waitForInVisibility(String data, String referenceValue) {
        wait = new WebDriverWait(driver,Long.parseLong(data));
        wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath(referenceValue)));
    }

    public static void main(String... ali) {
        String action = "some action";
        switch(action) {
        case "Load":
            loadThePage("some description", "some data");
            break;
        case "RefreshPage":
            refreshThePage("description", "data", "someValue", "someValue"));
            break;
        case "uploadFile":
            uploadTheFile("someData", "someReference", "some value");
            break;
        case "uploadImage":
            uploadTheImage("description", "data", "Some Reference", "some value");
            break;
        case "waitELVisibility":
            waitForVisibility("data", "some ref", "some value");
            break;
        case "waitElInvisibility":
            waitForInvisibility("data", "some value");
            break;
        }
    }
}

您随时可以通过传递值作为参数来重复使用这些方法。如果您不想将值作为参数传递,则将其删除,然后声明并初始化这些值 方法上方带有static关键字的变量,以后可以在方法内部使用它。

以下是上述switch-case语句的相应if-else条件:

if(action.equals("Load"))
{
    loadThePage("some description", "some data");
}
else if(action.equals("RefreshPage"))
{
    refreshThePage("description", "data", "someValue", "someValue"));
}
else if(action.equals("uploadFile"))
{
    uploadTheFile("someData", "someReference", "some value");
}
else if(action.equals("uploadImage"))
{
    uploadTheImage("description", "data", "Some Reference", "some value");
}
else if(action.equals("waitELVisibility"))
{
    waitForVisibility("data", "some ref", "some value");
}
else if(action.equals("waitElInvisibility"))
{
    waitForInvisibility("data", "some value");
}

希望对您有帮助...

答案 2 :(得分:-1)

通常,我不会使用静态方法。为了便于说明,我将它们设为静态并假定所有对象都是可访问的(所有类变量)。我也没有实现所有方法-仅原理图:

public static void main(String[] args){
    //...

    // i would use a switch for the dispatch
    switch(action){
      case "Load": handleLoad(); break;
      case "RefreshPage": handeRefreshPage(); break;
      case "InsertData" : insertData(); break;
      // ...
    }

    //...
}

// define the handlers for the actions as individual methods:

public static void handleLoad(){
    Reporter.log(description+"|"+data);
    driver.get(data);
    if(!TestBase.browserName.equals("Chrome")){
        driver.manage().window().maximize();
        screenSize=driver.manage().window().getSize().toString();
        System.out.println("My screensize is "+screenSize);
    }

}

public static void handleRefreshPage(){
    driver.navigate().refresh();                         
    wait.until(ExpectedConditions.visibilityOfElementLocated(                        
    By.xpath("//span[contains(text(),'salesforce.com, inc. All rights reserved.')]")));  
}

// and so on