简体   繁体   中英

How to pass the value from one method to another method

I have a doubt in Java selenium webdriver. For example: on page 1 (Personal Details page) I am filling all personal details and selecting the payment method (like credit or debit card) using a checkbox. On page 2 (Dispatch): filling the dispatch address details. On Page 3 (Payment Page): based on the selection from page 1 the payment should be selected automatically.

I created one class file with methods for this pages. But I dont know how to pass the value from one method to another.

Kindly help me to resolve this issue.

//Class File

package logintest;

//import org.openqa.selenium.By;

public class mobclas 
{
  private static WebElement element=null;

  public static class mobile
  {
    public static WebElement Personal_Details(WebDriver driver)
    {
       //Code 
       driver.findElement(By.id("Name")).sendKeys("Test");  
       driver.findElement(By.id("Mobile_No")).sendKeys("963258741");  
       driver.findElement(By.id("Mail_id")).sendKeys("test@test.com");
       //Payment method selection
       if checkbox true, we have to pass the value to "PaymentPage method"
    }

    public static WebElement DispatchDetails(WebDriver driver)
    {
       //Code
    }
    public static WebElement paymentpage(WebDriver driver)
    {
       //Value
       if true Credit card payment
       else Debit card payment
    }
  }
}

You have two options:

1.Pass values as method arguments 2.Create class property, and use it value inside all class methods.

Do you tried them?

完成详细信息后重定向到 servlet 并将值存储在会话中并重定向到下一页,以便最终您可以对所有先前的详细信息执行操作。

Here there is no clear answer,

  1. do you really need to pass this value? because you are writing test cases, so you must be knowing what value you are going to select on payment detail page, so create different methods like

    Personal_Details_CreditCard(should fill personalDetail and select CreditCard Option), Personal_Details_DebitCard(should fill personalDetail and Select DebitCard option), CreditCardPayment(Should perform creditcard payment steps Payment Page ), DebitCardPayment(should perform debitCard payment steps on payment Page)

    and call these methods in required order. like if you are calling Personal_Details_CreditCard then call CreditCardPayment for Payment page.

  2. If you want to save state variable, then you can use instance variables at class level if all methods are part of single class.

  3. You can also pass different arguments from your test method like Personal_Details(paymentType) , PaymentPage(PaymentType)

So in short there is no straighforward answer to your question, it is really a design issue and you should select a suitable design as per you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM