我有很多IF Else语句的单一方法的Java代码。下面是示例之一。如何转换成它们单独的方法,我需要重构这些IF Else语句到的方法,我在我的黄瓜,硒框架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)));
}
我要为那些如果Else语句的方法 - 加载RefreshPage InsertData uploadFile waitElVisibility waitElInVisibility
您可以在类单独的方法为每个否则,如果状态,这样,如果前进则需要再次进行这些动作,你可以直接使用的方法和执行的操作。我现在用私人的访问修饰符,但你可以把它公开,如果你想使用该方法的类之外也是如此。而不是使用否则,如果,你应该使用开关,这样,未来是否有其他动作发挥作用,所以只需要添加,在开关状态。
您的代码应该是这个样子:
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关键字这些变量,你可以在以后使用它里面的方法。
下面是相应的if-else条件以上的switch-case语句:
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");
}
我希望它可以帮助...
一般来说,我不会用静态方法。为了示范起见,我让他们静,假设所有的对象都可以访问(所有类变量)。此外,我并没有实现所有的方法 - 只是示意性的:
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