0

I try to create the next expression in codemodel(Sun):

  driver.findElement(By.xpath("//div[text()=\""+whatToclick+"\"]/parent::span/parent::span")).click();

so whatToclick would be a parameter in my function.

So I wrote the next:

    method.body().invoke(JExpr.ref("driver"), "findElement").arg(jc.ref(By.class).staticInvoke("xpath").arg("//div[text()=\"+"+ whatToclick.name() +"+\"]/parent::span/parent::span")).invoke("click");

and I have two problems:

  1. I don't success to put whatToClick as a parameter. It always in the quotation mark(Because my expression is a string, and I have to put /", otherwise it close the string.)

  2. The expression is created without the click() part.

Any help?

Or Smith
  • 3,556
  • 13
  • 42
  • 69

1 Answers1

1

Try the following instead, which uses the the JExpr.lit() to reference Strings directly and .add() to concatenate the strings and variables. Also it uses the add() method on the method body to add the click() invocation:

method.body().add(JExpr.ref("driver").invoke("findElement")
    .arg(codeModel.ref(By.class).staticInvoke("xpath")
        .arg(JExpr.lit("//div[text()=\"+").plus(whatToclick).plus(JExpr.lit("+\"]/parent::span/parent::span"))).invoke("click")));

outputs:

driver.findElement(By.xpath((("//div[text()=\"+"+ whatToclick)+"+\"]/parent::span/parent::span")).click());
John Ericksen
  • 10,995
  • 4
  • 45
  • 75