Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from selenium import webdriver
- class Facebook:
- # metodo que prepara o teste
- def setUp(self):
- self.driver = webdriver.Firefox()
- self.driver.implicitly_wait(30)
- self.base_url = "https://www.facebook.com/"
- # metodo que seleciona o form pelo id
- def get_form_by_id(self, driver, id_form):
- form = driver.find_element_by_id(id_form)
- # all_children_by_css = form.find_elements_by_id("email")
- # all_children_by_xpath = form.find_elements_by_xpath(".//*")
- # print(len(all_children_by_xpath))
- # for webElement in all_children_by_xpath:
- # print(webElement.tag_name, webElement.get_attribute("id"), webElement.get_attribute("class"))
- return form
- # metodo responsavel por realizar o teste
- def test_facebook(self):
- driver = self.driver
- driver.get(self.base_url + "/")
- form = self.get_form_by_id(driver, "login_form")
- all_children_by_xpath = form.find_elements_by_xpath(".//*")
- # codigo que procura os nós do tipo input que possuem a substring "login" como valor de algum de seus atributos
- for childWebElement in all_children_by_xpath:
- if(childWebElement.tag_name == "input"): # procuramos todos nós do tipo input (descendentes do form)
- childWebElement_attributes_dict = driver.execute_script( # pegamos todos atributos e valores do input e criamos um dicionario
- 'var attr = {}; ' #dicionario do JS que guardará os atributos
- 'for (index = 0; index < arguments[0].attributes.length; ++index) { ' #fazemos um loop pelo NamedNodeMap
- #arguments[0] retorna o primero parametro (argumento) passado para a função. Nesse caso trata-se do childWebElement
- #arguments[0].attributes trata-se de um "dicionario" (NamedNodeMap no JS) dos atributos
- 'attr[arguments[0].attributes[index].name] = arguments[0].attributes[index].value ' #preenchemos o dicionario attr {name:value}
- '}; return attr;', childWebElement)
- for chave, valor in childWebElement_attributes_dict.items(): # iteramos por todas posições do dicionario no Python (chave::valor)
- string_value = str(valor)
- if 'login' in string_value: # consultamos se o valor da dupla chave::valor da atual posição contem a substring 'login'
- print(childWebElement_attributes_dict) # mostramos na tela os atributos dos inputs que contem a subtring
- print()
- fbTest = Facebook()
- fbTest.setUp()
- fbTest.test_facebook()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement