mcdcorp / opentest

Open source test automation tool for web applications, mobile apps and APIs
https://getopentest.org
MIT License
447 stars 107 forks source link

How create IF/ELSE with script #591

Open Navitimex opened 1 year ago

Navitimex commented 1 year ago

I have tried to create an if but have not succeeded. If I write a simple condition like (3>4) it does the steps. But the condition I want is that if something is visible to perform another action, if it is not visible break the test case. I am planning to create a delete test case so I need to check that it exists before I can delete it.

      - description: if send keys Example
        script: |
            var2 = 
            ($runAction("org.getopentest.selenium.AssertElementVisible", {
              locator: { xpath: "//input[@id='edit-submit']" }
            })); 
            if (var2 === true ) {
              $runAction("org.getopentest.selenium.SendKeys", {
                locator: { xpath: "//input[@id='edit-name']" },
                text: "user-name" , 
                $screenshot: true
              });
            } else {
              $runAction("org.getopentest.selenium.SendKeys", {
                locator: { xpath: "//input[@id='edit-pass']" },
                text: "password-text", 
                $screenshot: true
              });
            }

// I have tried many ways, but I can't succeed.

      - description: Insert Username
        action: org.getopentest.selenium.SendKeys
        args:
          locator: { id: edit-name }
          text: automation
          if: $script: |
              $runAction("org.getopentest.selenium.AssertElementVisible", {
                locator: { xpath: "//input[@id='edit-submit']" }
              });
adrianth commented 1 year ago

From what you're describing, it sounds like you don't even need an if statement and you should be able to achieve this without using any JavaScript code:

- description: Verify that the Submit button exists
  action: org.getopentest.selenium.AssertElementVisible
  args:
    locator: { xpath: "//input[@id='edit-submit']" }

- description: Insert username
  action: org.getopentest.selenium.SendKeys
  args:
    locator: { id: edit-name }
    text: automation

If the Submit button doesn't exist, the test will fail at the AssertElementVisible action and never execute the SendKeys action. Let me know if this did the trick.