是否有任何方法可以在柏树中自动化这种情况,以便随机选择随机国家/地区/地区的下拉列表?

发布于 2025-02-09 04:21:11 字数 306 浏览 1 评论 0原文

我正在自动化页面。在下拉赛中,我每次运行测试案例都会随机选择一个国家。但是如何映射Math.random功能选择的随机国家,其中我将所有国家添加到数组中。

var country =[" "]
var randomItem = country[Math.floor(Math.random() * country.length)];

如果通过上述代码随机选择印度,我需要从第二个下拉列表中随机选择印度的任何一个州。

I am automating the page. in the drop-down, I am trying to choose a country randomly every time I run the test case. but how to map random country chosen by the math.random function where I added all the countries into the array.

var country =[" "]
var randomItem = country[Math.floor(Math.random() * country.length)];

If India is selected randomly by the above code, I need to choose any one of the states from India randomly from the second drop down.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很糊涂小朋友 2025-02-16 04:21:11

在固定装置文件夹中创建state.json文件。

{
   "India":[
      "State1",
      "State2",
      "State3"
   ],
   "US":[
      "State",
      "State2",
      "State3"
   ]
}

假设您的下拉元素是选择选项

//Select the Country
cy.get('option')
  .its('length')
  .then((len) => {
    //select by index
    cy.get('country-dropdown-selector').select(Math.floor(Math.random() * len))
  })

//Retrieve the innertext of selected option
cy.get('country-dropdown-selector')
  .invoke('text')
  .then((country) => {
    cy.fixture('state.json').then((state) => {
      //From the extracted country, go to json file,
      //and get the length of state array
      cy.get('state-dropdown-selector').select(
        Math.floor(Math.random() * state[`${country}`].length)
      )
    })
  })

Inside the Fixtures folder create a state.json file like this.

{
   "India":[
      "State1",
      "State2",
      "State3"
   ],
   "US":[
      "State",
      "State2",
      "State3"
   ]
}

Assuming your dropdown elements are select and option.

//Select the Country
cy.get('option')
  .its('length')
  .then((len) => {
    //select by index
    cy.get('country-dropdown-selector').select(Math.floor(Math.random() * len))
  })

//Retrieve the innertext of selected option
cy.get('country-dropdown-selector')
  .invoke('text')
  .then((country) => {
    cy.fixture('state.json').then((state) => {
      //From the extracted country, go to json file,
      //and get the length of state array
      cy.get('state-dropdown-selector').select(
        Math.floor(Math.random() * state[`${country}`].length)
      )
    })
  })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文