def test_selene_demo(self): tasks = browser.all("#todo-list>li") active_tasks = tasks.filtered_by(have.css_class("active")) browser.open(app_url) browser.should(have.js_returned(True, is_TodoMVC_loaded)) for task_text in ["1", "2", "3"]: browser.element("#new-todo").set_value(task_text).press_enter() tasks.should(have.texts("1", "2", "3")).should_each(have.css_class("active")) browser.element("#todo-count").should(have.text('3')) tasks[2].element(".toggle").click() active_tasks.should(have.texts("1", "2")) active_tasks.should(have.size(2)) tasks.filtered_by(have.css_class("completed")).should(have.texts("3")) tasks.element_by(not_(have.css_class("completed"))).should( have.text("1")) tasks.filtered_by(not_(have.css_class("completed"))).should( have.texts("1", "2")) browser.element(by.link_text("Active")).click() tasks[:2].should(have.texts("1", "2")) tasks[2].should(be.hidden) browser.element(by.id("toggle-all")).click() browser.element("//*[@id='clear-completed']").click() tasks.should(be.empty)
def test_search_is_updated_on_next_actual_action_like_questioning_count( session_browser): page = GivenPage(session_browser.driver) page.opened_empty() elements = session_browser.all('li').filtered_by( have.css_class('will-appear')) page.load_body(''' <ul>Hello to: <li>Anonymous</li> <li class='will-appear'>Bob</li> <li class='will-appear'>Kate</li> </ul>''') assert len(elements) == 2 page.load_body(''' <ul>Hello to: <li>Anonymous</li> <li class='will-appear'>Bob</li> <li class='will-appear'>Kate</li> <li class='will-appear'>Joe</li> </ul>''') assert len(elements) == 3
def test_waits_nothing(session_browser): page = GivenPage(session_browser.driver) page.opened_empty() elements = session_browser.all('li').filtered_by( have.css_class('will-appear')) page.load_body(''' <ul>Hello to: <li>Anonymous</li> <li class='will-appear'>Bob</li> <li class='will-appear' style='display:none'>Kate</li> </ul>''') assert len(elements) == 2 page.load_body_with_timeout( ''' <ul>Hello to: <li>Anonymous</li> <li class='will-appear'>Bob</li> <li class='will-appear' style='display:none'>Kate</li> <li class='will-appear'>Joe</li> </ul>''', 500, ) assert len(elements) == 2
def test_search_is_lazy_and_does_not_start_on_creation(session_browser): page = GivenPage(session_browser.driver) page.opened_empty() non_existent_collection = session_browser.all('.not-existing').filtered_by( have.css_class('special') ) assert str(non_existent_collection)
def test_search_is_updated_on_next_actual_action_like_questioning_displayed( session_browser, ): page = GivenPage(session_browser.driver) page.opened_empty() element = session_browser.all('.will-appear').element_by( have.css_class('special') ) page.load_body( ''' <ul>Hello to: <li class="will-appear">Bob</li> <li class="will-appear special">Kate</li> </ul>''' ) assert element().is_displayed() is True page.load_body( ''' <ul>Hello to: <li class="will-appear">Bob</li> <li class="will-appear special" style="display:none">Kate</li> </ul>''' ) assert element().is_displayed() is False
def get_product_info(self, element: [Element]) -> ProductInfo: element.should(have.css_class('inventory_item')) return ProductInfo( element.element("img.inventory_item_img").get_property('src'), element.element("div.inventory_item_img > a").get_property('href'), element.element("div.inventory_item_name").get(query.text), element.element("div.inventory_item_label > a").get_property( 'href'), element.element("div.inventory_item_desc").get(query.text), element.element("div.inventory_item_price").get(query.text), )
def test_completed_tasks(): browser.element('[id="new-todo"]').type('a').press_enter() browser.element('[id="new-todo"]').type('b').press_enter() browser.element('[id="new-todo"]').type('c').press_enter() browser.all('#todo-list>li').should(have.exact_texts('a', 'b', 'c')) browser.element('#todo-list>li:nth-child(2) .toggle').click() browser.element('#todo-list>li:nth-child(2)').should( have.css_class('completed')) browser.element('#todo-list>li:nth-child(1)').should( have.no.css_class('completed')) browser.element('#todo-list>li:nth-child(3)').should( have.no.css_class('completed'))
def test_counts_invisible_tasks(session_browser): page = GivenPage(session_browser.driver) page.opened_empty() elements = session_browser.all('li').filtered_by( have.css_class('will-appear')) page.load_body(''' <ul>Hello to: <li>Anonymous</li> <li class='will-appear'>Bob</li> <li class='will-appear' style='display:none'>Kate</li> </ul>''') assert len(elements) == 2
def test_complete_task(): # browser.config.driver = # browser.config.hook_wait_failure=\ # lambda error: TimeoutException(error.msg.replace('file://', '')) # browser.config.timeout = 6 # browser.config.save_screenshot_on_failure = False # browser.config.save_page_source_on_failure = False browser.open('http://todomvc.com/examples/emberjs/') browser.element('#new-todo').type('a').press_enter() browser.element('#new-todo').type('b').press_enter() browser.element('#new-todo').type('c').press_enter() # browser.element('#new-todo').with_(Config(timeout=2)).should(have.value('foo')) browser.all('#todo-list>li').should(have.exact_texts('a', 'b', 'c')) browser.all('#todo-list>li').element_by(have.exact_text('b'))\ .element('.toggle').click() browser.all('#todo-list>li').filtered_by(have.css_class('completed'))\ .should(have.exact_texts('b')) browser.all('#todo-list>li').filtered_by(have.no.css_class('completed'))\ .should(have.exact_texts('a', 'c'))
def should_have_active(self, *todos: str): self._list.filtered_by(have.css_class('active')).should( have.exact_texts(*todos)) return self
from selene import have from selene.support.shared import browser from selene.support.shared.jquery_style import s, ss # Arrange browser.config.hold_browser_open = True # Задержка браузера browser.open("http://todomvc.com/examples/emberjs/") # Act s('[id="new-todo"]').type( 'listen lesson').press_enter() # Поиск Элемента по ID s('[id="new-todo"]').type('have a rest').press_enter() # Ввод текста s('[id="new-todo"]').type('do home work').press_enter() # Assert ss('[id="todo-list"] li').should( have.exact_texts('listen lesson', 'have a rest', 'do home work')) # Подсчет элементов s('[id="todo-list"] li:nth-child(2) .toggle').click() browser.element('[id="todo-list"] li:nth-child(2)').should( have.css_class('completed')) browser.element('[id="todo-list"] li:nth-child(1)').should( have.no.css_class('completed')) browser.element('[id="todo-list"] li:nth-child(2)').should( have.no.css_class('completed'))
def start_editing(todo: str, new_text): todo_list.element_by(have.exact_text(todo)).double_click() return todo_list.element_by(have.css_class('editing'))\ .element('.edit').perform(command.js.set_value(new_text))
def start_editing(text, new_text): todo(text).double_click() return todos.element_by(have.css_class('editing'))\ .element('.edit').set_value(new_text)
from selene import have from selene.support.shared import browser be_completed = have.css_class('completed') class TodoMvc: def __init__(self): self.todo_list = browser.all('#todo-list>li') def open(self): browser.open('https://todomvc4tasj.herokuapp.com/') app_wait = "return $._data($('#clear-completed')[0], 'events')"\ ".hasOwnProperty('click')" browser.should(have.js_returned(True, app_wait)) return self def create(self, *todos: str): for todo in todos: browser.element('#new-todo').type(todo).press_enter() return self def given_opened_with(self, *todos: str): self.open() self.create(*todos) def should_have(self, *todos: str): self.todo_list.should(have.exact_texts(*todos)) return self def start_editing(self, todo, new_text):
def start_editing(self, todo, new_text): self.todo_list.element_by(have.exact_text(todo)).double_click() return self.todo_list.element_by(have.css_class('editing'))\ .element('.edit').with_(set_value_by_js=True).set_value(new_text)
def remove_product_from_cart(self, element: [Element]): self.should_be_opened() element.should(have.css_class('inventory_item')) element.element("button.btn_secondary.btn_inventory").should( have.exact_text("REMOVE")).click() return self
def add_product_to_cart(self, element: [Element]): self.should_be_opened() element.should(have.css_class('inventory_item')) element.element("button.btn_primary.btn_inventory").should( have.exact_text("ADD TO CART")).click() return self
def active_todos_should_be(self, *names: str): self.todos.filtered_by(have.css_class('active'))\ .should(have.exact_texts(*names)) return self
def start_editing(self, name: str, new_name): self.todos.element_by(have.exact_text(name)).double_click() return self.todos.element_by(have.css_class('editing'))\ .element('.edit').with_(set_value_by_js=True).set_value(new_name)
def should_have_completed(self, *todos: str): self._list.filtered_by(have.css_class('completed')).should( have.exact_texts(*todos)) return self
def start_editing(text, added_text): todo_list.element_by(have.exact_text(text)). \ double_click() return todo_list.element_by(have.css_class('editing')). \ element('.edit').perform(command.js.set_value(added_text))
def start_edit(name: str, new_name: str): todo(name).double_click() return todos.element_by(have.css_class('editing')).element('.edit').set_value(new_name)
def completed_todos_should_be(self, *names: str): self.todos.filtered_by(have.css_class('completed'))\ .should(have.exact_texts(*names)) return self
from selene import have from selene.support.shared import browser browser.config.hold_browser_open = True browser.open("http://todomvc.com/examples/emberjs/") browser.element('[id="new-todo"]').type('a').press_enter() browser.element('[id="new-todo"]').type('b').press_enter() browser.element('[id="new-todo"]').type('c').press_enter() browser.all('#todo-list>li').should(have.exact_texts('a', 'b', 'c')) browser.element('#todo-list>li:nth-child(2) .toggle').click() browser.element('#todo-list>li:nth-child(2)').should(have.css_class('completed')) browser.element('#todo-list>li:nth-child(1)').should(have.no.css_class('completed')) browser.element('#todo-list>li:nth-child(3)').should(have.no.css_class('completed')) #browser.close()
return self def should_have_validation_for(self, data=None, /, **kwargs): """ check that validation is present on fields with ellipsis ... as value """ data_dict = { key: value for key, value in { **(data or NoneObject()).__dict__, **kwargs, }.items() if value is ... } for key in data_dict: self.context.element(key).should(have.css_class('o_field_invalid')) return self def should_have_filled(self, data, /, **kwargs): # TODO: implement return self def submit(self, data=None, /, *, by_button: Union[SeleneElement, str, List[Union[SeleneElement, str]], ] = '[type=submit]', **kwargs):
from selene import have from selene.support.shared import browser browser.config.hold_browser_open = True browser.open("http://todomvc4tasj.herokuapp.com/") browser.should(have.js_returned(True, "return $._data($('#clear-completed')[0], 'events').hasOwnProperty('click')")) # Add browser.element('#new-todo').type('a').press_enter() browser.element('#new-todo').type('b').press_enter() browser.element('#new-todo').type('c').press_enter() browser.all('#todo-list>li').should(have.exact_texts('a', 'b', 'c')) # Edit browser.all('#todo-list>li').element_by(have.exact_text('b')).double_click() browser.all('#todo-list>li').element_by(have.css_class('editing')).element('.edit').type(' edited').press_enter() # Complete & Clear browser.all('#todo-list>li').element_by(have.exact_text('b edited')).element('.toggle').click() browser.element('#clear-completed').click() browser.all('#todo-list>li').should(have.exact_texts('a', 'c')) # Cancel Edit browser.all('#todo-list>li').element_by(have.exact_text('c')).double_click() browser.all('#todo-list>li').element_by(have.css_class('editing')).element('.edit').type('to be canceled').press_escape() # Delete browser.all('#todo-list>li').element_by(have.exact_text('c')).hover().element('.destroy').click() browser.all('#todo-list>li').should(have.exact_text('a'))