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

how to use format painter multiple times in word
how to use format painter multiple times in excel
how to use format painter multiple times mcq
how to use format painter multiple times in powerpoint
how to make same format in word
format painter for multiple rows
activate format painter for multiple use
how to format multiple word documents at once

I have a java code with lots of IF Else statements in a single method. Below is the sample one. How do i convert them into individual methods, I need to refactor those IF Else statements into Methods, and I have to use these refactored methods in my StepDefinition in Cucumber-Selenium framework.

Can someone please help me with this...

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

I have to create a method for those If Else statements - Load RefreshPage InsertData uploadFile waitElVisibility waitElInVisibility

You can make separate methods in the class for every else if condition so that if going forward you need those actions to be performed again, you can directly use the method and perform the action. I am using private access modifier now but you can make it public if you want to use that method outside the class as well. And instead of using else if, you should use switch, so that in future if any other action comes into play, so just need to add that in the switch condition.

Your code should look something like:

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

How to use Format Painter to format more than one item or selection , To use the Format Painter to copy the formatting to multiple selections, follow these steps: Click to select the item that has the formatting that you  One of the simplest methods is to make multiple selections before applying any formatting. Just hold down the Ctrl key while you use the mouse to select different sections of text (or other elements in your documents, such as pictures), then apply the formatting. Each item you’ve selected will receive the same formatting.

You can split if-else blocks contents into multiple methods and if you want to get rid of if-else then the alternative is, you can use the switch statement. See the code below :

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

You can re-use these methods whenever you want to by passing values are parameters. If you don't want to pass values as parameters then remove it and then declare & initialize those variables with static keyword on above the methods and you can use it inside of method later.

Below is the corresponding if-else conditions for the above switch-case statements :

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

I hope it helps...

How to format multiple charts quickly, Learn how to copy and paste chart formats from one chart to another. You will be able to save hours of time with this little Excel trick. As a recap – here’s how to format multiple sheets at the same time: 1. Ctrl + Click each sheet tab at the bottom of your worksheet (selected sheets will turn white). 2.

In general I wouldn't use static methods. For sake of the example I made them static and assume that all objects are accessible(all class variables). Also I did not implement all methods - only schematic:

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

Formatting Multiple Documents (Microsoft Word), Formatting Multiple Documents For instance, if there is a Body Text format in both the document and the template, the style definitions in the  How to format multiple charts quickly Here is the chart format we wish to copy: We can click anywhere on the chart. Then click Home -> Copy (or Ctrl + C)

Multiple Number Formatting < Thought, The plan here is to create the correct formatting without using VBA code or Excel formulae in ONE cell so that if any of the above values in the left hand column  Use Format Painter Multiple Times To copy the format from one cell to multiple places, without repeatedly needing to click the Format Painter icon: 1.Select the cell 2.

Use Format Painter Multiple Times, To copy the format from one cell to multiple places, without repeatedly needing to click the Format Painter icon: 1.Select the cell. 2.Double-Click the Format  You can format multiple bars by selecting the tasks, click the Format ribbon, and click the Format drop-down box and select Bar. Another suggestion: Use the marked field and using the bar styles command to change the formatting of all marked tasks. Add the marked field to the view and set the field to "Yes". Go the format ribbon and select Bar Styles.

Excel Conditional Formatting, Multiple Conditions. You can include up to three different conditions on any cell or range of cells. For example your three conditions might be: 0) If the number  Click the Use A Formula To Determine Which Cells To Format option in the top pane. Enter the formula =OR ($E5< ($D5-$C$2),$E5> ($D5+$C$2)) in the lower pane. Click Format and specify what you want.

Comments
  • Have answered your question, please check and let me know if it works.
  • Thank you so much for your help!
  • Happy to help you :)
  • appreciate your help, yes I will construct methods the same way as you mentioned
  • Thank you Kai for your help!