Selenium IDE: Check if checkbox is ticked, if not then check



  • I seem to be looking in the wrong direction so perhaps someone can help me.

    In my testcase i need to have 1 specific checkbox 'on' at all times, i need some kind of if/else loop imo but can't figure it out. Can someone come to the rescue?

    enter image description here



  • enter image description here

    A simple if statement works like given above, you could try yourself.

    If you change the target value to true for if command then the output will be Passed and if you keep it as false, the output will be fail.

    Coming to your question

    if you want to verify whether a checkbox is selected. You can use the verify checked command.

    But, these commands do not have any return value and so, cannot be used for conditional statements.

    For controlling the flow according to checkbox status, you can use execute script command to verify the 'checked' property of the element:

    Add the below command in IDE:

    command: execute script
    Target : return (document.querySelectorAll('[name="selected(1234)"]'))[0].checked
    value: test
    

    Note: queryselector is the function to identify an element using CSS locator, full list of supported element locators could be found at :

    https://www.w3schools.com/js/js_htmldom_elements.asp

    Note that few locators return arrays and few returns single element. You should handle it accordingly. for instance, query selector returns an array of 1 element so I took [0] the first element.

    Final code would look like:

    enter image description here

    .side file:

    Just copy the below code and save it as test.side file. Now open it in IDE and run it and see how the control flow works.

    {
      "id": "e775f634-710f-41a4-9d86-ed26d93f5f78",
      "version": "2.0",
      "name": "sdad",
      "url": "http://book.theautomatedtester.co.uk",
      "tests": [{
        "id": "81f96aae-1436-4766-9005-f868d5350de4",
        "name": "sd",
        "commands": [{
          "id": "50ff0f41-4ec0-41fa-992f-d2ec9d4f6381",
          "comment": "",
          "command": "open",
          "target": "/chapter1",
          "targets": [],
          "value": ""
        }, {
          "id": "a8a9e9b1-1d61-49a3-ab18-5e6598e5c1ed",
          "comment": "",
          "command": "setWindowSize",
          "target": "1897x1040",
          "targets": [],
          "value": ""
        }, {
          "id": "408fc4ff-6bbd-4c42-b8d3-f09ca43a5b45",
          "comment": "",
          "command": "select",
          "target": "id=selecttype",
          "targets": [],
          "value": "label=Selenium Core"
        }, {
          "id": "163049da-7205-440d-923c-51356cc5f22b",
          "comment": "",
          "command": "//click",
          "target": "name=selected(1234)",
          "targets": [],
          "value": ""
        }, {
          "id": "f6f8739c-86c6-40ea-a235-eeb8c7c2f374",
          "comment": "",
          "command": "executeScript",
          "target": "return (document.querySelectorAll('[name=\"selected(1234)\"]'))[0].checked",
          "targets": [],
          "value": "test"
        }, {
          "id": "d7952432-7499-4173-b7bd-e09663163708",
          "comment": "",
          "command": "echo",
          "target": "${test}",
          "targets": [],
          "value": ""
        }, {
          "id": "0e83238a-ec38-4247-b1e6-485bd81c680a",
          "comment": "",
          "command": "if",
          "target": "${test}",
          "targets": [],
          "value": ""
        }, {
          "id": "6f832f5c-31df-41f1-a399-c7348095f614",
          "comment": "",
          "command": "echo",
          "target": "Passed",
          "targets": [],
          "value": ""
        }, {
          "id": "6d2f22db-11b5-43b8-a56e-1ef7991fd503",
          "comment": "",
          "command": "else",
          "target": "",
          "targets": [],
          "value": ""
        }, {
          "id": "c77edb7b-22df-4507-91bc-f03c7e71bddb",
          "comment": "",
          "command": "echo",
          "target": "fail",
          "targets": [],
          "value": ""
        }, {
          "id": "30c78100-ab95-4382-8ae2-b98ee8d60826",
          "comment": "",
          "command": "end",
          "target": "",
          "targets": [],
          "value": ""
        }]
      }],
      "suites": [{
        "id": "cb4118db-162f-4ca5-a002-ceb87733dbba",
        "name": "Default Suite",
        "persistSession": false,
        "parallel": false,
        "timeout": 300,
        "tests": ["81f96aae-1436-4766-9005-f868d5350de4"]
      }],
      "urls": ["http://book.theautomatedtester.co.uk/"],
      "plugins": []
    }
    

    Final code looks like:

    enter image description here

    Now you can see how test flow changes by running the test with step 4 enabled and then disabled


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2