Q - เกิดerror โค้ด ขึ้นว่า AttributeError: 'TextTestResult' object has no attribute 'openwed_Login'

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

ธวัชชัย แสนหาญ
PHP Super Member
PHP Super Member
โพสต์: 499
ลงทะเบียนเมื่อ: 15/11/2018 10:02 am

Q - เกิดerror โค้ด ขึ้นว่า AttributeError: 'TextTestResult' object has no attribute 'openwed_Login'

โพสต์โดย ธวัชชัย แสนหาญ » 15/02/2019 5:54 pm

Q - เกิดerror โค้ด ขึ้นว่า AttributeError: 'TextTestResult' object has no attribute 'openwed_Login'
Error แจ้งว่า ไม่มีแอตทริบิวต์ 'openwed_Login'
ต้องทำยังไงโค้ดถึง จะสามารถรู้จัก ฟังก์ชัน 'openwed_Login' ครับ

รูปภาพ

Code Error

โค้ด: เลือกทั้งหมด

Traceback (most recent call last):
  File "D:/Python selenium_mt27/test_11.py", line 628, in <module>
    runner.run(MooZiiCart_check.categories_check_Add)
  File "C:\Users\ter\AppData\Local\Programs\Python\Python36\lib\unittest\runner.py", line 176, in run
    test(result)
  File "D:/Python selenium_mt27/test_11.py", line 544, in categories_check_Add
    self.openwed_Login(pw1,self.base_url,self.username,self.pwd)
AttributeError: 'TextTestResult' object has no attribute 'openwed_Login'


โค้ดที่ใช้รัน

โค้ด: เลือกทั้งหมด

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.select import Select
import time, base64, re, requests ,unittest,os





user = 'mt27'
password = 'mt27007'
user1 = "ธวัชชัย แสนหาญ"
password1 = "0929462162"
user2 ='mt77'
password2 ='87654321'
path = r'E:\geckodriver-v0.23.0-win64\geckodriver.exe'
wed0 = "http://mzc.buildinstore.com/administrator/index.php"
wed1 = "http://mzc.buildinstore.com/index.php/en/"
wed_mindphp = "https://www.mindphp.com/forums/viewforum.php?f=342" #https://www.mindphp.com/forums/posting.php?mode=post&f=342
firefoxpath = "/home/com015/geckodriver"
Toggle  = "//a[contains(.,'Toggle editor')]"
new = '.button-new'
save = '.button-apply'
save_close ='.button-save'
Cancel ='.button-cancel'
Public ='.button-publish'
Unpublic ='.button-unpublish'
delete ='#toolbar-delete > button.btn.btn-small'
Detail ='Details'
pw1 ='passwd'
pw2 ='password'
'categories'
CategoryTitle = 'Book'
CategoryTitle1 = 'Book'
Alias ='book'
Short_description = 'book'
Description = 'book'
Access ='Public'
Category_sorting ='First'
Amount_of_products_on_page ='10'
Amount_of_products_in_row ='5'
Parent_category ='Top level'
categoriesfile_edit ='book'
'Product'
ProductTitle = 'One Piece'
ProductTitle1 = 'One Piece2'
ProductAlias = 'One Piece'
ProductShort_description = 'One Piece'
ProductDescription = 'One Piece'
access = 'Public'
price = '30'
currency = 'บาท'
Amount_of_product_in_stock = '20'
tax_id = 'Normal (5.00%)'
category_id = 1
lable = 'No'
Productfile_edit = 'One Piece'
'Clients'
ClientsUsername = 'MT001'
ClientsUsername1 = 'MT0011'
ClientsEmail = 'MT001@gmail.com'
ClientsPassword = '543210'
ClientsUsergroup = 'group A'
Clientsfile_edit = 'MT001'
'Manufacturers'
ManufacturersTitle = 'test Grand Home (Chaina) Co.,Ltd.'
ManufacturersTitle1 = 'Grand Home (Chaina) Co.,Ltd.'
ManufacturersAlias = 'Grand Home'
ManufacturersShort_description = 'Grand Home (Chaina)'
ManufacturersDescription = 'Grand Home (Chaina) Co.,Ltd.'
ManufacturersURL = 'www.GrandHome.co.th'
ManufacturersAmount_of_products_on_page ='10'
ManufacturersAmount_of_products_in_row ='10'
Manufacturersfile_edit = 'test Grand Home (Chaina) Co.,Ltd.'

class joomla(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox(executable_path=path)  # __builtin__.firefoxpath)
        self.base_url = wed0  # __builtin__.base_url #=
        self.base_url1 = wed1  # __builtin__.base_url1 #=
        self.username = user  # __builtin__.username #=
        self.username1 = user1  # __builtin__.username1 #=
        self.username2 = user2  # __builtin__.username1 #=
        self.pwd = password  # __builtin__.pwd #=
        self.pwd1 = password1  # __builtin__.pwd1 #=
        self.pwd2 = password2  # __builtin__.pwd1 #=
        self.verificationErrors = []
        self.accept_next_alert = True
        self.wait = WebDriverWait(self.driver, 10)
        # self.driver.maximize_window()




    def msg_error(self):
        for i in range(5):
            try:
                if self.driver.find_element_by_css_selector("div.alert.alert-error"):
                    obj = self.driver.find_element_by_css_selector("div.alert.alert-error")
                    msg = obj.text
                    self.msg = msg
                    break
            except:
                pass
            print(i)
            time.sleep(1)
        else:
            msg = "ไม่มี"
            self.msg = msg


    def report_error(self):
        'Report'
        Error_test = self.case
        Error_test1 = str(Error_test)
        driver = self.driver
        # obj = self.driver.find_element_by_xpath("/html/body")
        # msg = obj.text

        self.msg_error()
        Url = self.driver.current_url  # เก็บ URL ไว้ในตัวแปร
        snap = self.driver.get_screenshot_as_base64()
        text = str(snap)#snap
        base = "data:image/png;base64," + text
        url = "http://snap.mindphp.com/index.php"
        files = {'paths': base, 'upload': 'upload', 'from_python': 'true'}
        response = requests.post(url, data=files)
        url_img = response.text
        url_snap_img = "http://snap.mindphp.com/index.php%s" % url_img

        self.driver.get(wed_mindphp)
        for i in range(4):
            try:
                if self.driver.find_element_by_name("username"):
                    login = self.wait.until(ec.visibility_of_element_located((By.NAME, "username")))
                    ActionChains(self.driver).move_to_element(login).perform()
                    a = self.driver.find_element_by_name("username")
                    a.clear()
                    a.send_keys(user1)
                    a = self.driver.find_element_by_name("password")
                    a.clear()
                    a.send_keys(password1)
                    time.sleep(3)
                    self.driver.find_element_by_name("login").click()
                    time.sleep(5)
                    print('login')
                    break

            except:
                if self.driver.find_element_by_link_text("ตั้งกระทู้ใหม่"):
                    print("ตั้งกระทู้ใหม่")
                    break

                pass
            print("ทำใหม่")
            time.sleep(2)
        else:
            print("error")
        self.driver.find_element_by_link_text("ตั้งกระทู้ใหม่").click()
        a = self.driver.find_element_by_name("subject")
        a.clear()
        a.send_keys("B - เว็บระบบของ Moozii cart มีปัญหาที่ %s" % Error_test1)
        a = self.driver.find_element_by_name("message")
        a.clear()
        a.send_keys("[color=#FF0000][size=150]เกิดปัญหา moozii cart ขัดข้อง ที่ Case  %s[/size][/color]\n"% Error_test1)
        a.send_keys('\n')
        a.send_keys('ข้อความError : [color=#FF0000]%s[/color] \n' % self.msg)
        a.send_keys('ตำแหน่งที่ Error : [color=#FF0000]%s[/color] \n' % self.msg1)
        a.send_keys('link error : %s \n' % Url)
        a.send_keys('user : %s\n' % user)
        a.send_keys('password : %s\n' % password)
        a.send_keys('[img]%s[/img]\n' % url_snap_img)
        # self.driver.find_element_by_name("post").click()
        self.driver.find_element_by_name("preview").click()
        time.sleep(15)
        driver.quit()

    def openwed_Login(self,elementpwd,wed,user,password):
        'Login เข้าระบบ Joomla'
        self.case = self.openwed_Login.__doc__

        self.driver.get(wed)
        driver = self.driver

        for i in range(10):
            try:

                if self.driver.find_element_by_name("username"):
                    break
                print(self.driver.get(wed))


            except:
                pass
            print("ทำใหม่")
            time.sleep(2)
        else:
            print("error")
            self.msg1 = 'wed error'
            self.report_error()
            driver.quit()
        login = self.wait.until(ec.visibility_of_element_located((By.NAME, "username")))
        ActionChains(self.driver).move_to_element(login).perform()
        a = self.driver.find_element_by_name("username")
        a.clear()
        a.send_keys(user)
        a = self.driver.find_element_by_name(elementpwd)
        a.clear()
        a.send_keys(password)
        a = self.driver.find_element_by_css_selector(".btn-primary")
        a.submit()
        time.sleep(5)

    def menu_MooZiiCart(self,menu):
        menu = menu
        driver = self.driver
        self.wait.until(ec.visibility_of_element_located((By.LINK_TEXT, "Components")))
        self.driver.find_element_by_link_text("Components").click()

        for i in range(10):
            try:
                if self.driver.find_element_by_link_text("MooZiiCart"):break
            except:
                pass
            print("หาใหม่")
            time.sleep(2)
        else:
            print("error")
            self.msg1 = 'MooZiiCart'
            self.report_error()
            driver.quit()
        time.sleep(2)
        self.driver.find_element_by_link_text("MooZiiCart").click()

        for i in range(10):
            try:
                if self.driver.find_element_by_xpath("//span[contains(.,'%s')]"%menu): break
            except:
                pass
            time.sleep(2)
        else:
            print ("report bug")
            self.msg1 = menu
            self.report_error()
            driver.quit()
        time.sleep(3)
        self.driver.find_element_by_xpath("//span[contains(.,'%s')]" % menu).click()
    def xpath_click(self, element):
        for i in range(10):
            try:
                if self.driver.find_element_by_xpath(element):break
            except:
                pass
            print('x%d' % i)
            time.sleep(2)
        else:
            print ("Error")
            self.msg1 = element
            self.report_error()
            self.driver.quit()
        time.sleep(3)
        self.driver.find_element_by_xpath(element).click()

    def find(self, element):
        for i in range(10):
            try:
                if self.driver.find_element_by_xpath(element):break
            except:
                pass
            print('x%d' % i)
            time.sleep(2)
        else:
            print ("Error")
            self.msg1 = element
            self.report_error()
            self.driver.quit()
        time.sleep(3)
        element1 = self.driver.find_element_by_xpath(element)
        self.driver.execute_script("arguments[0].click();", element1)

    def find_css(self, element):
        for i in range(10):
            try:
                if self.driver.find_element_by_css_selector(element): break
            except:
                pass
            time.sleep(2)
        else:
            print("Error")
            self.msg1 = element
            self.report_error()
            self.driver.quit()
        time.sleep(3)
        self.driver.find_element_by_css_selector(element).click()

    def find_linktext(self, element):
        for i in range(10):
            try:
                if self.driver.find_element_by_link_text(element): break
            except:
                pass
            time.sleep(2)
        else:
            print("Error")
            self.msg1 = element
            self.report_error()
            self.driver.quit()
        time.sleep(3)
        self.driver.find_element_by_link_text(element).click()

    def Confirm(self):
        obj = self.driver.switch_to.alert
        msg = obj.text
        print("Alert shows following message: " + msg)
        time.sleep(2)
        obj.accept()
        print(" Clicked on the OK Button in the Alert Window")
        time.sleep(5)

    def information_name (self,element,information):
        for i in range(10):

            try:
                if self.driver.find_element_by_name(element):
                    a = self.driver.find_element_by_name(element)
                    a.clear()
                    a.send_keys(information)
                    break
            except:
                pass
            print("pass%d"%i)

            time.sleep(2)
        else:
            print ("Error")
            self.msg1 = element
            self.report_error()
            self.driver.quit()
        time.sleep(3)

    def open_file(self,file):
        for i in range(10):
            try:
                if self.driver.find_element_by_xpath("//a[contains(.,'%s')]" % file):
                    break
            except:
                pass
            print('หาใหม่')

            time.sleep(1)
        else:
            print("Error")
            self.msg1 = file
            self.report_error()
            self.driver.quit()
        time.sleep(3)
        element1 = self.driver.find_element_by_xpath("//a[contains(.,'%s')]" % file)
        self.driver.execute_script("arguments[0].click();", element1)
    def check_file(self,file):
        for i in range(10):
            try:
                if self.driver.find_element_by_xpath("//a[contains(.,'%s')]" % file):
                    break
            except:
                pass
            print('check file : %d' % i)

            time.sleep(1)
        else:
            print("Error")
            self.msg1 = file
            self.report_error()
            self.driver.quit()
        print('check pass')

    def Dropdown(self,element,text):
        for i in range(10):
            try:
                if self.driver.find_element_by_name(element):break
            except:
                pass
            time.sleep(2)
        else:
            print ("Error")
            self.msg1 = element
            self.report_error()
            self.driver.quit()
        time.sleep(3)
        select = Select(self.driver.find_element_by_name(element))
        select.select_by_visible_text(text)

    def box(self, boxtr ,boxtd):
        print("xpath=//form[@id='adminForm']/table/tbody/tr[%d]/td[%d]/input"  %(boxtr,boxtd))
        for i in range(10):
            try:
                if self.driver.find_element_by_xpath("//form[@id='adminForm']/table/tbody/tr[%d]/td[%d]/input"  %(boxtr,boxtd)):
                    element1 = self.driver.find_element_by_xpath("//form[@id='adminForm']/table/tbody/tr[%d]/td[%d]/input" % (boxtr, boxtd))
                    self.driver.execute_script("arguments[0].click();", element1)
                    break
            except:

                pass
            time.sleep(2)
        else:
            print('error box')
            self.msg1 = "box%d"%boxtr
            self.report_error()
            self.driver.quit()

    def menu_buy (self,buy):
        for i in range(10):
            try:
                if self.driver.find_element_by_xpath("(//a[contains(text(),'Buy')])[%d]" %buy):break
            except:
                pass
            time.sleep(2)
        else:
            print ("Error")
            self.msg1 = "Buy%d"%buy
            self.report_error()
            self.driver.quit()
        time.sleep(3)
        element1 = self.driver.find_element_by_xpath("(//a[contains(text(),'Buy')])[%d]" %buy)
        self.driver.execute_script("arguments[0].click();", element1)


    def information_css (self,element,information):
        for i in range(10):
            try:
                if self.driver.find_element_by_css_selector(element):break
            except:
                pass
            print("pass")
            time.sleep(2)
        else:
            print ("Error")
            self.msg1 = element
            self.report_error()
            self.driver.quit()
        time.sleep(3)
        a = self.driver.find_element_by_css_selector(element)
        a.send_keys(information)

    def check_Delete(self, check):
        for i in range(5):
           try:
               if self.driver.find_element_by_css_selector(".row0:nth-child(1) #cb0"):break

           except:
               pass
           time.sleep(1)
        else:
            self.msg1 = "wed error"
            self.report_error()
            self.driver.quit()
        for i in range(5):
            try:
                if self.driver.find_element_by_xpath("//a[contains(.,'%s')]" % check):
                    print('c1')
                    self.report_error()
                    break
            except:

                pass
        else:
            print('check pass')
            self.driver.quit()

    def check_show(self ,box ,td):

        n = box % 2
        if n == 1:
            m = n - 1
        else:
            m = n + 1

        print('.row%d:nth-child(%d) > td:nth-child(%d) > a'% (m, box, td))
        for i in range(10):
            try:
                if self.driver.find_element_by_css_selector('.row%d:nth-child(%d) > td:nth-child(%d) > a'% (m, box, td)):
                    ojb = self.driver.find_element_by_css_selector('.row%d:nth-child(%d) > td:nth-child(%d) > a' % (m, box,td))
                    msg = ojb.text
                    self.msg1 = msg                          #.row0:nth-child(3) > td:nth-child(3) > a
                    break
            except:
                pass
            print(i)
            time.sleep(1)
        else:
            print('error check show')
            self.msg1 = 'ไม่มี box%d'%box
            self.report_error()
            self.driver.quit()

    def check_show_P_A_D(self, box, td):



        print('.row0:nth-child(%d) > td:nth-child(%d) > a' % ( box, td))
        for i in range(10):
            try:
                if self.driver.find_element_by_css_selector('.row0:nth-child(%d) > td:nth-child(%d) > a' % ( box, td)):
                    ojb = self.driver.find_element_by_css_selector('.row0:nth-child(%d) > td:nth-child(%d) > a' % (box, td))
                    msg = ojb.text
                    self.msg1 = msg  # .row0:nth-child(3) > td:nth-child(3) > a
                    break
            except:
                pass
            print(i)
            time.sleep(1)
        else:
            print('error check show')
            self.msg1 = 'ไม่มี box%d' % box
            self.report_error()
            self.driver.quit()

    def Options(self):
        'Options'
        self.openwed_Login(pw1, self.base_url, self.username, self.pwd)
        self.case = self.Options.__doc__
        self.menu_MooZiiCart('Options')

    def Configuration(self):
        'Options'
        self.openwed_Login(pw1, self.base_url, self.username, self.pwd)
        self.case = self.Options.__doc__
        self.menu_MooZiiCart('Configuration')

class MooZiiCart_check(joomla):



    def categories_check_Add(self):
        'Add categories'

        self.driver = webdriver.Firefox(executable_path=path)

        self.openwed_Login(pw1,self.base_url,self.username,self.pwd)
        self.case = self.categories_check_Add.__doc__
        self.menu_MooZiiCart("Categories")
        self.find_css(new)
        self.find(Toggle)
        self.information_name('name_en-GB',CategoryTitle)
        self.information_name('alias_en-GB',Alias)
        self.information_name('short_description_en-GB', Short_description)
        self.information_name('description1', Description)
        self.find_linktext('Description (th)')
        self.information_name('name_th-TH', CategoryTitle)
        self.information_name('alias_th-TH', Alias)
        self.information_name('short_description_th-TH', Short_description)
        self.find("//*[@id='th-TH-page']/div[1]/table/tbody/tr[4]/td[2]/div/div[2]/div/a")
        self.information_name('description2', Description)
        self.find_linktext(Detail)
        self.Dropdown('access',Access)
        self.Dropdown('ordering',Category_sorting)
        self.information_name('products_page',Amount_of_products_on_page)
        self.information_name('products_row',Amount_of_products_in_row)
        self.Dropdown('category_parent_id',Parent_category)
        self.find_css(save_close)
        self.check_file(CategoryTitle)
        self.driver.quit()

    def Product_check_Add(self):
        'Add Product'
        self.driver = webdriver.Firefox(executable_path=path)
        self.openwed_Login(pw1, self.base_url, self.username, self.pwd)
        self.case = self.Product_check_Add.__doc__
        self.menu_MooZiiCart('Products')
        self.find_css(new)
        self.find(Toggle)
        self.information_name('name_en-GB', ProductTitle)
        self.information_name('alias_en-GB', ProductAlias)
        self.information_name('short_description_en-GB', ProductShort_description)
        self.information_name('description1', ProductDescription)
        self.find_linktext('Description (th)')
        self.information_name('name_th-TH', ProductTitle)
        self.information_name('alias_th-TH', ProductAlias)
        self.information_name('short_description_th-TH', ProductShort_description)
        self.find("//*[@id='th-TH-page']/div[1]/table/tbody/tr[4]/td[2]/div/div[2]/div/a")
        self.information_name('description2', ProductDescription)
        self.find_linktext(Detail)
        self.Dropdown('access', access)
        self.information_name('product_price', price)
        self.Dropdown('currency_id', currency)
        self.information_name('product_quantity', Amount_of_product_in_stock)
        self.Dropdown('product_tax_id', tax_id)
        self.find_css("#category_id > option:nth-child(%d)" % category_id)
        self.Dropdown('label_id', lable)
        self.find_css(save_close)
        self.check_file(ProductTitle)
        self.driver.quit()

    def Orders_check_Add(self):
        'Add Orders'
        self.driver = webdriver.Firefox(executable_path=path)
        self.openwed_Login(pw1, self.base_url, self.username, self.pwd)
        self.case = self.Orders_check_Add.__doc__
        self.menu_MooZiiCart('Orders')
        self.find_css(new)
        self.find_css(".span10 > input")
        self.Dropdown('user_id', 'MT27 mt27')
        self.find("//input[@value='Add Product']")
        self.find("(//a[contains(text(),'Load')])[2]")

        self.find('//*[@id="adminForm"]/table/tbody/tr[1]/td[3]/b/a')
        time.sleep(5)
        self.information_name('order_subtotal', '200')
        self.find_css(save_close)

        self.driver.quit()


       
testcase = 1

if testcase != 0:
    print('เข้า')

    if testcase == 1 :
        print('เข้า1')
        runner = unittest.TextTestRunner()
        runner.run(MooZiiCart_check.categories_check_Add)

    elif testcase == 2 :
        print('เข้า2')
        runner = unittest.TextTestRunner()
        runner.run(MooZiiCart_check.Product_check_Add)

    elif testcase == 3 :
        print('เข้า3')
        runner = unittest.TextTestRunner()
        runner.run(MooZiiCart_check.Orders_check_Add)


  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ย้อนกลับไปยัง

ผู้ใช้งานขณะนี้

กำลังดูบอร์ดนี้: 7 และ บุคคลทั่วไป 0 ท่าน