Транспортир - Нажатие кнопки в div, которая имеет интервал с определенным текстом - PullRequest
0 голосов
/ 03 июля 2018

У меня проблема с нажатием кнопки в транспортире. Упрощенный код выглядит так:

<div class = "container">   
  <div class = "col1">
    <span class = "hour">12:00</span>   
  </div>

  <div class="col2">
    <button class="btn">Test</button>   
  </div>
</div>

<div class = "container">   
      <div class = "col1">
        <span class = "hour">13:00</span>   
      </div>

      <div class="col2">
      <button class="btn">Test</button>   
      </div>    
</div>

Я бы хотел нажать кнопку в div, где его интервал имеет "12:00" час. Состояние должно проверить час.

Я могу легко проверить диапазон с

by.xpath('//span[contains(text(), "12:15")]')

Моя главная задача - выбрать кнопку с родительским div, имеющим этот диапазон. Это возможно? Спасибо.

Ответы [ 2 ]

0 голосов
/ 04 июля 2018

by.xpath('//span[contains(text(), "12:15")]/ancestor::div//button[@class='btn']') Надеюсь, этот ответ поможет вам !!

0506

0 голосов
/ 04 июля 2018

У меня было похожее состояние. Но мне нужно было просто извлечь значение из DOM и сравнить его с ожидаемым результатом, это выглядит так:

records = await targeting_page.records.getText();
        recordsNumber = parseFloat(records.replace(/,/g, ''));
        recordsRange = recordsNumber >= 100 && recordsNumber < 90000000;
        await expect(recordsRange).toEqual(true);

но ваш случай не большая разница.

то, что я написал в консоли браузера на этой странице вашего вопроса

    async function fin() {
    for(var i = 0; i < 100; i++) {
    var myElement = document.querySelector("code span:nth-child(27)").textContent //your find your desired element
    console.log(b)
    typeof b == 'string' ? console.log(true) : console.log(false) //this is optionally 

    var res = parseFloat(b) //parse when it will be 12:00 you will get the 12 number
    console.log(res)
    res == 13 ? await b.click() : console.log("The button is not ready yet") //and here compare is there that number you need, if yes then click element(this doesn't work in console, 
// if you use async/await in your protractor tests, there are it will work
     }    
    }

    fin()

Вот что касается непосредственно этой страницы. В тесте это будет выглядеть примерно так (может быть, намного лучше, но я очень быстро его написал):

for(let i=0; i < 100; i++) {
var myE = await myElement.getText(); //where myElement -- your selector
        var myNumber = parseFloat(myE.replace(/:/g, '')); // parse and get 12 out of "12:00"
        if (myNumber == 12) {
await myElement.click()
 } else {
  console.log("The button is not ready yet")
 }
}
...