Haider T
Haider T

Reputation: 77

How do I use a variable for multiple method in selenium?

Issue: I have a local variable that I need to use for other method with in the parent class. I have used instance variable but I am getting an error for my send keys

Thing I have tried/experimented: I have declared an instance variable name String "String AppID;"

Error I am getting from my instance variable experiment: org.openqa.selenium.WebDriverException: unknown error: keys should be a string

Here is the Full code:

  public class NewWarrant extends TestBase {

Solicitation solicitation;
WorkloadManager workloadmanager;

String AppID; // Instance Variable Declared
public NewWarrant() {
    super();
}

public void createNewWarrantPage() throws Exception {

    /
    WebDriverWait wait = new WebDriverWait(driver, 40);

      // Navigating to the Warrant Page
      driver.findElement(Transaction_Link).click();
      driver.findElement(Acquisitions_Link).click();
      driver.findElement(Additional_Form_Link).click();
      driver.findElement(New_Link).click();
      driver.findElement(Warrent_link).click(); 
    // switching to page Iframe
      WebElement iframe = driver.findElement(By.xpath("//*[@id='PegaGadgetIfr']"));
      driver.switchTo().frame(iframe); // Filling out all data's for the page
      driver.findElement(Warrent_Template_Field).sendKeys("CLASS_I");
      //Sending the newly created KO User to Canidate ID field
      synchoWait();
      driver.findElement(By.id("CandidateOpID")).sendKeys(Keys.chord(Keys.CONTROL, "v"));
      synchoWait();
      driver.findElement(By.xpath("//*[@id='po0']")).click();
      synchoWait(); 
      driver.findElement(DoDDAC_Input_Field).sendKeys("W91QV1");
      driver.findElement(PCO_CheckBox).click(); synchoWait();
      driver.findElement(Limited_Radio_Button).click();
      driver.findElement(Prejudice_radio_Button).click();
      synchoWait();
      driver.findElement(Semester_radio_Button).click();
      synchoWait();
      driver.findElement(Supervisor_Field).sendKeys("dschrute"); 
      synchoWait();
      driver.findElement(New_Warrant_Submit_Button).click();
       synchoWait();
    //Using the AppID instance variable to store the value using  enterTextInField method.
      AppID = getTextOfElement();
      driver.switchTo().defaultContent();
      driver.findElement(Profile_Dropdown).click();
      driver.findElement(Sign_Out).click(); 



}

public void reviewAppByCanidate() throws Exception {

    try {
        synchronized (Task_Input_Field) {
            Task_Input_Field.wait(2000);
            driver.findElement(Task_Input_Field).sendKeys("Candidate Warrant Review");
            }
    } catch (Exception e) {
        driver.findElement(Task_Input_Field).sendKeys("Candidate Warrant Review");
    }
     synchoWait();
     // I am using App ID instance variable with enterTextInField method to send the values in to the field.
    enterTextInField(AppID);
    driver.findElement(Search_Button).click();
    driver.findElement(Sort_Task_Button).click();
    driver.findElement(Sort_By_Field).sendKeys("Assignment Date");
    synchoWait();
    driver.findElement(Sort_Decending_Arrow).click();
    synchoWait();
    driver.findElement(Sort_Button).click();
    synchoWait();
    driver.findElement(Box_Card).click();
    synchoWait();
    driver.findElement(Open_Button).click();
    synchoWait();
    WebElement iframe = driver.findElement(By.xpath("//*[@id='PegaGadgetIfr']"));
    driver.switchTo().frame(iframe); // Filling out all data's for the page
    driver.findElement(Cand_Review_Submit_Button).click();
    driver.switchTo().defaultContent();
    driver.findElement(Profile_Dropdown).click();
    driver.findElement(Sign_Out).click(); 
         }

 // Get Text of Element to store in variable call "text"
        public String getTextOfElement()  {
            //WebDriverWait wait = new WebDriverWait(driver, 30);
            //wait.until(ExpectedConditions.visibilityOfElementLocated(Application_ID));
             String text = driver.findElement(Application_ID).getText();
            driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
            System.out.println("My copied value: " + text);
            // return elementText;
            return text;
        }


        // Enter Text of element from stored variable from getTextOfElement()
        public void enterTextInField(String value)  {
            driver.findElement(Item_Number_Field).clear();
            driver.findElement(Item_Number_Field).sendKeys(value);
             System.out.println("value copied");
        }
}

Console Screen shot enter image description here

Step Defination class screen shot enter image description here

Feature File screen shot enter image description here

Upvotes: 1

Views: 1017

Answers (2)

Hitesh Sharma
Hitesh Sharma

Reputation: 11

Try to perform the same using actions. It may work and if you want to perform paste action then try to capture the text in variable then pass it to send keys its the better solution instead of copy pasting.

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168157

You cannot pass a variable between Cucumber steps as its value is getting cleared each time new step is being executed.

  1. The easiest solution is to declare it as static, however this way you can run into issues if you will be running your test in parallel mode.
  2. You can come up with a custom implementation of Scenario Context
  3. You can go for world object implementation

In any case consider refactoring your test to use Page Object Model design pattern to introduce abstraction layer between test logic and DOM elements definitions.

Upvotes: 2

Related Questions