如何将多个 'if else' 语句格式化为 selenium java 项目中的 "public static void" 方法

How to format multiple 'if else' statements into "public static void" methods in selenium java project

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

有人可以帮我解决这个问题吗...

else if(action.equals("Load"))
{
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);
}

}else if(action.equals("RefreshPage"))
{
driver.navigate().refresh();                         
wait.until(ExpectedConditions.visibilityOfElementLocated(                        
By.xpath("//span[contains(text(),'salesforce.com, inc. All rights 
reserved.')]")));
}

else if(action.equals("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);
}
}
else if(action.equals("uploadFile"))
{
File file = new File(data);
String filePath=file.getAbsolutePath();
System.out.println(filePath);
findElement(elementReference, referenceValue).clear();
findElement(elementReference, referenceValue).sendKeys(filePath);
}
else if(action.equals("uploadImage"))
{
Reporter.log(description+"||"+data);
uploadImage(elementReference, referenceValue, "Logo", new File(data));
}
else if(action.equals("waitElVisibility"))
{
wait = new WebDriverWait(driver,Long.parseLong(data));
wait.until(ExpectedConditions.visibilityOf(findElement(elementReference, 
referenceValue)));
}
else if(action.equals("waitElInVisibility"))
{
wait = new WebDriverWait(driver,Long.parseLong(data));
wait.until(ExpectedConditions.invisibilityOfElementLocated(
By.xpath(referenceValue)));
}

我必须为那些 If Else 语句创建一个方法 - 加载 刷新页面 插入数据 上传文件 等待可见性 waitElInVisibility

您可以在 class 中为每个 else if 条件创建单独的方法,这样如果您需要再次执行这些操作,您可以直接使用该方法并执行该操作。我现在正在使用 private 访问修饰符,但如果你想在 class 之外使用该方法,你也可以将其设置为 public。而不是使用 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");
    }
}

您可以将 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");
}

希望对您有所帮助...

一般来说我不会使用静态方法。为了示例的缘故,我将它们设为静态并假设所有对象都是可访问的(所有 class 变量)。我也没有实现所有的方法——只有原理图:

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