如何在不等待 30 秒超时的情况下测试元素的缺失 [英] How to test for absence of an element without waiting for a 30 second timeout

查看:32
本文介绍了如何在不等待 30 秒超时的情况下测试元素的缺失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些功能测试,做一些简单的单页测试需要 5 分钟,因为 find_element 函数在未找到元素时需要 30 秒才能完成.我需要测试元素的缺失,而不必等待超时.我一直在搜索,但到目前为止还没有找到 find_element() 的任何替代方法.这是我的代码:

I am writing some functional tests and it takes 5 minutes to do some simple single-page testing because the find_element function takes 30 seconds to complete when the element is not found. I need to test for the absence of an element without having to wait for a timeout. I've been searching but so far have not found any alternative to find_element(). Here's my code:

def is_extjs_checkbox_selected_by_id(self, id):
    start_time = time.time()
    find_result =  self.is_element_present(By.XPATH, "//*[@id='" + id + "'][contains(@class,'x-form-cb-checked')]")  # This line is S-L-O-W
    self.step(">>>>>> This took " + str( (time.time() - start_time) ) + " seconds")
    return find_result

def is_element_present(self, how, what):
    try: self.driver.find_element(by=how, value=what)
    except NoSuchElementException, e: return False
    return True

谢谢.

好吧,我遵循了此处和其他链接中的大部分建议,最终未能实现目标.它具有完全相同的行为,即在未找到元素时花费 30 秒:

Well, I followed most of the recommendations here and in the other links and in the end, and have failed to achieve the goal. It has the exact same behavior of taking 30 seconds when the element is not found:

# Fail
def is_element_present_timeout(self, id_type, id_locator, secs_wait_before_testing):
    start_time = time.time()
    driver = self.driver
    time.sleep(secs_wait_before_testing)
    element_found = True
    try:
        element = WebDriverWait(driver, 0).until(
            EC.presence_of_element_located((id_type, id_locator))
        )
    except:
        element_found = False
    elapsed_time = time.time() - start_time
    self.step("elapsed time : " + str(elapsed_time))
    return element_found

这是使用获取所有元素的想法的第二种方法

Here's a second approach using the idea of getting all elements

# Fail
def is_element_present_now(self, id_type, id_locator):
    driver = self.driver
    # This line blocks for 30 seconds if the id_locator is not found, i.e. fail
    els = driver.find_elements(By.ID, id_locator)
    the_length = els.__len__()
    if the_length == 0:
        result = False
    else:
        result = True
    self.step('length='+str(the_length))
    return result

注意,我没有接受之前的答案,因为遵循海报的建议没有产生成功的结果.

Note, I've unaccepted previous answer since following poster's advice did not produce a successful result.

推荐答案

在对此进行研究但一无所获后,我终于想出了一个可接受的解决方案.解决这个问题是多么容易,这有点令人尴尬.我所要做的就是使用implicitly_wait() 函数来操作超时,出于某种原因,我忽略了/没有注意到.我正在使用 Louis 提供的语法,但它会更简单,并且通过采用上面的原始 is_element_present() 函数并在 find_element() 函数之前和之后添加 driver.implicitly_wait() 函数来工作.

After researching this and getting nowhere I finally came up with an acceptable solution. It's kind of embarrassingly simple how easy this was to solve. All I had to do was to manipulate the timeout with the implicitly_wait() function, which for some reason I was ignoring/not noticing. I'm using the syntax provided by Louis but it would be even simpler and would work just the same by taking my original is_element_present() function above and adding the driver.implicitly_wait() functions before and after the find_element() function.

# Success
def is_element_present(self, id_type, id_locator):
    driver = self.driver
    element_found = True
    driver.implicitly_wait(1)
    try:
        element = WebDriverWait(driver, 0).until(
            EC.presence_of_element_located((id_type, id_locator))
        )
    except:
        element_found = False
    driver.implicitly_wait(30)
    return element_found

这篇关于如何在不等待 30 秒超时的情况下测试元素的缺失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆