Esempio n. 1
0
    def DateReset(self,
                  original_date,
                  target_date,
                  source_folder,
                  target_folder,
                  format="0"):
        u'''
        format is 0 : 2017-01-02
        format is 1 : 20170102
        format is 2 : 2017/01/02

        find out original date,then change to target date and create target folder

        Examples:
        | Date Reset | 2015-08-18 | 2015-08-30 | sourcefolder | targetfolder |
        '''
        self.format = int(format)
        try:
            sourcefolder = CONTEXT.get_path(source_folder)
            targetfolder = CONTEXT.get_path(target_folder)
            Common.mkdir(targetfolder)
            for root, dirs, files in os.walk(sourcefolder):
                for name in files:
                    newlines = []
                    f = open(os.path.join(root, name), 'r')
                    lines = f.readlines()
                    f.close()
                    for line in lines:
                        match = re.findall(datafomat[self.format][0], line)
                        if match:
                            for d in match:
                                days = self._daysdiff(d, original_date)
                                newdate = self._get_day_of_targetday(
                                    target_date,
                                    int(str(days).replace('+', '')))
                                line = line.replace(d, str(newdate))
                        newlines.append(line)
                    if not os.path.exists(
                            os.path.join(
                                root.replace(sourcefolder, targetfolder))):
                        os.mkdir(
                            os.path.join(
                                root.replace(sourcefolder, targetfolder)))
                    newfile = open(
                        os.path.join(root.replace(sourcefolder, targetfolder),
                                     name), 'w')
                    newfile.writelines(newlines)
        except Exception, e:
            print 'DateReset error:%s' % e
Esempio n. 2
0
    def DB_Execute_Sql(self, sql, host=_Host, user=_User, password=_Password):
        u'''
        Default Value:
        user = '******'  passwrod = 'derbysoft'
        
        Examples:

        | DB Execute Sql  | ./sample.sql |
     
        '''
        sql = CONTEXT.get_path(sql)
        newlines, status = Common.rendefile(sql)
        if status:
            newsql = Common.CreateTempFile(newlines)
            command = """mysql -h%s -u%s -p%s < "%s" """ % (host, user,
                                                            password, newsql)
        else:
            command = """mysql -h%s -u%s -p%s < "%s" """ % (host, user,
                                                            password, sql)
        dlog.write(command)
        m = subprocess.Popen(command,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             shell=True)
        errormessage = m.stderr.read()
        m.wait()
        if errormessage != "":
            dlog.fail(errormessage)
        else:
            dlog.write("DB Execute Sql is successfully")
Esempio n. 3
0
    def Test_Data(self, pattern, path=None):
        u'''
        get test folder or file
        return List
        
        | @{files}=  | Test Data |   .*.txt  |  
        | @{files}=  | Test Data |   .*.txt  |  shop/TC01.001 |

        '''
        if path is None:
            test_path = CONTEXT.get_test_case_path()
        else:
            test_path = CONTEXT.get_path(path)
        dlog.write(test_path)
        datas = []
        for fn in os.listdir(test_path):
            match = re.findall(pattern, fn)
            if match:
                datas.append(fn)
        if len(datas) == 0:
            dlog.write(
                "Can't find defined files,you can reference this link(http://10.200.107.43/wiki/en/Automation_File_Format"
            )
            return datas
        datas.sort(cmp=mycmp)
        datas = [os.path.join(test_path, d) for d in datas]
        return datas
Esempio n. 4
0
    def properties_change(self, path, dict):
        u'''
        Examples: 
        
        | properties change  |  ../classes/ccs.properties  |  {"ccs.url":"file://./setupdata/CCS/abc"}  |

        '''
        if type(dict) is unicode:
            dict = eval(dict)
        p = Properties()
        p.load(open(CONTEXT.get_path(path)))
        for k, v in dict.items():
            if p.has_key(k):
                p[k] = v
            else:
                dlog.write("path dosen't exist this key" % k, level='WARN')
        p.store(open(CONTEXT.get_path(path), 'w'))
Esempio n. 5
0
 def Compress_Gzip(self, data):
     u'''
     
     Compress data to gzip
     
     Examples: 
     | ${gzip}=    | Compress Gzip  |   "data"   |
     | ${gzip}=    | Compress Gzip  |   file.xml  |
     '''
     if Common.check_file(CONTEXT.get_path(data)):
         data = open(CONTEXT.get_path(data)).read()
     buf = StringIO()
     g = gzip.GzipFile(mode="wb", fileobj=buf)
     try:
         g.write(data)
     finally:
         g.close()
     return buf.getvalue()
Esempio n. 6
0
 def SendEmail(self, from_addr, pwd, to_addr, subject, message, *attach,
               **kwargs):
     u'''
     
     Send Email
     
     default smtp server:mail.derbysoft.com
     
     Examples: 
     
     | SendEMail  | from_email_address | password | to_email_address | email_subject | email_body | /attach1.xml | /attach2.xml |
     
     
     change smtp server 
     
     | SendEMail  | from_email_address | password | to_email_address | email_subject | email_body | /attach1.xml | /attach2.xml | smtp=xxx.xxx.com |
     
     change smtp ssl server 
     
     | SendEMail  | from_email_address | password | to_email_address | email_subject | email_body | /attach1.xml | /attach2.xml | smtp_ssl=xxx.xxx.com |
     
     '''
     msg = MIMEMultipart()
     smtpserver = 'mail.derbysoft.com'
     msg["Subject"] = subject
     msg["From"] = from_addr
     msg["To"] = to_addr
     part = MIMEText(message)
     msg.attach(part)
     for af in attach:
         part = MIMEApplication(open(CONTEXT.get_path(af), 'rb').read())
         part.add_header('Content-Disposition',
                         'attachment',
                         filename=os.path.split(CONTEXT.get_path(af))[-1])
         msg.attach(part)
     if kwargs.has_key('smtp'):
         s = smtplib.SMTP(kwargs['smtp'].encode('utf-8'))
     elif kwargs.has_key('smtp_ssl'):
         s = smtplib.SMTP_SSL(kwargs['smtp_ssl'].encode('utf-8'))
     else:
         s = smtplib.SMTP_SSL(smtpserver)
     s.login(from_addr, pwd)
     s.sendmail(from_addr, to_addr, msg.as_string())
     s.close()
Esempio n. 7
0
    def Match(self, datas, patterns, values, namespaces=None, method='x'):
        u'''
        method : x(xpath) , j(jsonpath)

        Examples:

        Set ResponseMatch by Xpath
        | ${rs1}=     | Match | ${requests} | ['ht:HotelIdInfo[1]/@id']    | ['890']  | {"ht":"http://schemas.xmlsoap.org/soap/envelope/"} |
        | ${rs2}=     | Match | ${requests} | ['//ht:Header//@id','//hot1:CheckIn[1]']  | ['903','2013-12-30']  | {"ht":"http://schemas.xmlsoap.org/soap/envelope/","ht1":"http://schemas.tourico.com/webservices/hotelv3"} |

        | ${rs1}=     | Match | ${requests} | ['.']    | ['com.derbysoft.storage.remote.dto.SaveDailyRateRequest'] |



        Set ResponseMatch by JsonPath
        | ${rs1}=     | Match  | ${requests} | ['$..hotel.name']     | ['BW']    | method=j |
        | ${rs2}=     | Match  | ${requests} | ['$..hotel.name',''$..hotel.id']   | ['BW'.'1234'] |  method=j |


        =========================================================================================================================================================================================================================================
        |       XPath           |          JSONPath             |                     Result                                         | 
        | /store/book/author    |    $.store.book[*].author     |     the authors of all books in the store                          |
        | //author              |    $..author                  |     all authors                                                    | 
        | /store/*              |    $.store.*                  |     all things in store, which are some books and a red bicycle.   |
        | /store//price         |    $.store..price             |     the price of everything in the store.                          |
        | //book[3]             |    $..book[2]                 |     the third book                                                 |
        | //book[last()]        |    $..book[(@.length-1)]      |                                                                    |
        |                       |    $..book[-1:]               |     the last book in order.                                        |
        | //book[position()<3]  |    $..book[0,1]               |                                                                    |
        |                       |    $..book[:2]                |     the first two books                                            |
        | //book[isbn]          |    $..book[?(@.isbn)]         |     filter all books with isbn number                              |
        | //book[price<10]      |    $..book[?(@.price<10)]     |     filter all books cheapier than 10                              |
        | //*                   |    $..*                       |     all Elements in XML document. All members of JSON structure.   |

        '''
        if type(datas) is not list:
            dlog.fail("The parameter datas must be a list")
        for d in datas:
            if Common.check_file(CONTEXT.get_path(d)):
                d = open(CONTEXT.get_path(d)).read()
            rm = ResponseMatch('', patterns, values, None, namespaces, method)
            rm.match(data(d))
            if rm.status:
                return d
Esempio n. 8
0
 def get_absolute_path(self, relative_path):
     u'''
     
     get file or folder absolute path
     
     Examples: 
     
     | ${path)  | get absolute path   |  ./setup/sql.xml   |
     | ${path)  | get absolute path   |  testfolder  |
     '''
     return CONTEXT.get_path(relative_path)
Esempio n. 9
0
 def Test_Template_Render(self, testtemplate, testdata, *valuename):
     result = []
     tmp_dir = CONTEXT.get_path("%s/%s" % (CONTEXT.get_suite_name(), 'template'))
     sys.path.append(tmp_dir)
     tmp_value = __import__(testdata)
     env = Environment(loader=FileSystemLoader(tmp_dir))
     template = env.get_template(testtemplate)
     for value in valuename:
         result.append(template.render(getattr(tmp_value, value)).encode('utf-8'))
     sys.path.remove(tmp_dir)
     return result
Esempio n. 10
0
 def Get_Json_Value(self, json_data, query):
     u'''
     Get json Values by query
     
     json_data:file or string
     
     _*Examples:*_
     
     data :
     
     {
       "locations": [
         {"name": "Seattle", "state": "WA"},
         {"name": "New York", "state": "NY"},
         {"name": "Bellevue", "state": "WA"},
         {"name": "Olympia", "state": "WA"}
       ]
     }
     
     | ${value}= | Get Json Value | data |  locations[0].state |
     | ${value} : WA |
 
     json query reference:
     
     [http://jmespath.org/|jmespath.org]
     
     [http://jmespath.org/tutorial.html|jmespath.org.tutorial]
     
     '''
     if Common.check_file(CONTEXT.get_path(json_data)):
         js = json.loads(open(CONTEXT.get_path(json_data)).read(),
                         encoding='utf-8')
     else:
         js = json.loads(json_data, encoding='utf-8')
     expression = jmespath.compile(query)
     result = expression.search(js)
     if type(result) is unicode:
         return result
     else:
         return json.dumps(result, ensure_ascii=False)
Esempio n. 11
0
    def Render_Template_To_File(self, template, context, newfile):
        u'''
        Args:
            template: filename
            context: template value
            newfile: file name
        Returns: newfile path

        Examples:
        | ${result} |  Render Template To File |  template.xml | {"date":"2017-01-01"} |  test.csv |
        Template Engine: JinJia2
        http://docs.jinkan.org/docs/jinja2/templates.html
        '''
        newfile_path = CONTEXT.get_path(newfile)
        if type(context) is unicode:
            context = eval(context)
        temp = CONTEXT.get_path(template)
        if Common.check_file(temp):
            fstring = self._FileSystemLoader(temp, context)
        else:
            fstring = self._StringLoader(template, context)
        open(newfile_path, 'w').write(fstring)
        return newfile_path
Esempio n. 12
0
 def Render_All_Templates(self, templates, context):
     if type(context) is unicode:
         context = eval(context)
     mytemplate = []
     result = []
     if type(templates) is list:
         mytemplate = templates
     else:
         mytemplate.append(templates)
     for temp in mytemplate:
         t = CONTEXT.get_path(temp)
         if Common.check_file(t):
             result.append(self._FileSystemLoader(t, context))
         else:
             result.append(self._StringLoader(temp, context))
     return result
Esempio n. 13
0
 def _ccs_duration_check(self, dur, ccsfile, jsonquery):
     while 1:
         try:
             if dur.status:
                 return
             ccs = json.loads(open(CONTEXT.get_path(ccsfile)).read(),
                              encoding='utf-8')
             expression = jmespath.compile(jsonquery)
             r = expression.search(ccs)
             if len(dur.result) == 0:
                 dur.result.append(r)
             elif str(r) != str(dur.result[-1]):
                 dur.result.append(r)
             time.sleep(0.01)
         except Exception, e:
             dlog.write(e)
Esempio n. 14
0
 def _run_jetty(self, port, apps, kwargs):
     app = []
     for a in apps:
         p_path = CONTEXT.get_path(a)
         app.append(""" "%s" """ % p_path)
         self._create_jetty_web_xml(os.path.basename(p_path), p_path,
                                    kwargs)
     app = ' '.join(app)
     command = """java -jar -Dfile.encoding=utf-8 "%s" --port %s  %s """ % (
         env.jetty_runner, port, app)
     print command
     if mswindows:
         self.process = subprocess.Popen(command, stderr=subprocess.PIPE)
     else:
         self.process = subprocess.Popen(command,
                                         stderr=subprocess.PIPE,
                                         shell=True)
Esempio n. 15
0
    def render(self, templatefile, context, save=True):
        u'''
        Examples:

        | Render |  template.xml  | {"timestamp":"12323213"}  |

        Template Engine: tenjin
        '''
        dlog.write(context)
        templatefile = CONTEXT.get_path(templatefile)
        if type(context) is unicode:
            #             context = json.loads(context, encoding="utf-8")
            context = eval(context)
        path = '\\'.join((templatefile.split('\\'))[:-1])
        tfile = templatefile.split('\\')[-1]
        engine = tenjin.Engine(path=[path], cache=tenjin.MemoryCacheStorage())
        newstring = engine.render(tfile, context)
        return newstring
Esempio n. 16
0
 def properties_read(self, path, keys):
     u'''
     Examples: 
     
     | @{pros}= | properties read  |  ../classes/ccs.properties  | ['ccs.url',hotel.topic]  |
     | ${ccsurl}=     | @{pros}[0]  | 
     | @{hoteltopic}= | @{pros}[1]  |
     '''
     if type(keys) is unicode:
         keys = ast.literal_eval(keys)
     p = Properties()
     p.load(open(CONTEXT.get_path(path)))
     rkeys = []
     for key in keys:
         if p.has_key(key):
             rkeys.append(p[key])
         else:
             dlog.write("path dosen't exist this key" % key, level='WARN')
     return rkeys
Esempio n. 17
0
 def _run_go(self, app):
     opath = os.getcwd()
     app = CONTEXT.get_path(app)
     dlog.write(app, console=True)
     os.chdir(os.path.dirname(app))
     if mswindows:
         command = "%s" % os.path.basename(app)
         print command
         self.process = subprocess.Popen(command,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.STDOUT)
     else:
         app = app.replace('.exe', '')
         command = "%s" % os.path.basename(app)
         print command
         self.process = subprocess.Popen(command,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.STDOUT,
                                         shell=True)
     os.chdir(opath)
Esempio n. 18
0
    def Render_Template(self, template, context):
        u'''
        Args:
            template: filename
            context: template value
        Returns: String

        Examples:
        | ${result} |  Render_Template  |  template.xml | {"date":"2017-01-01"} |

        Template Engine: JinJia2
        http://docs.jinkan.org/docs/jinja2/templates.html
        '''
        if type(context) is unicode:
            context = eval(context)
        temp = CONTEXT.get_path(template)
        if Common.check_file(temp):
            return self._FileSystemLoader(temp, context)
        else:
            return self._StringLoader(template, context)
Esempio n. 19
0
def CreateTempFile(content):
    if CONTEXT.get_variables('${TEST_NAME}') is not None:
        filename = "%s_%s" % (CONTEXT.get_variables('${TEST_NAME}'),
                              datetime.utcnow().strftime('%S%f'))
    elif CONTEXT.get_variables('${SUITE_NAME}') is not None:
        filename = "%s_%s" % (CONTEXT.get_variables('${SUITE_NAME}'),
                              datetime.utcnow().strftime('%S%f'))
    else:
        filename = "%s_%s" % ("tempfile", datetime.utcnow().strftime('%S%f'))
    folder = os.path.join(CONTEXT.get_path('.'), '_cache')
    mkdir(folder)
    path = os.path.join(folder, filename)
    print "This is cache file:%s" % (path)
    f = open(path, 'w')
    if type(content) is str:
        f.write(content)
    elif type(content) is unicode:
        f.write(str(content))
    elif type(content) is list:
        f.writelines(content)
    f.close()
    return path
Esempio n. 20
0
 def _getresponse(self, response):
     if Common.check_file(CONTEXT.get_path(response)):
         return open(CONTEXT.get_path(response), 'rb').read()
     else:
         return response
Esempio n. 21
0
 def ChangeJsonValue(self, jsonfile, patterns, values):
     u'''
     Examples:
     Before Json File:
     test.json = {
             "errorMessage": null,
             "value": {
             "HY": ["true",
             {
                 "__disabled": false,
                 "disabled": false,
                 "__updated": 1444901673000,
                 "description": "HYATT",
                 "created": 1401171574000,
                 "ratePlanMappingRequired": "true",
                 "updated": 1444901673000,
                 "gtaCode": "HY",
                 "__created": 1401171574000,
                 "derbyCode": "HYATT",
                 "cacheHour": "",
                 "taxCalRequired": "true",
                 "childAsAdultRequired": "true"
             }]
         }
         "success": true
     }
     
     | Change Json Value | test.json |  ["$..HY[0]","$..description"]  |  ["false","this is test"]  |
     
     Changed Json File:
     test.json={
             "errorMessage": null,
             "value": {
             "HY": ["false",
             {
                 "__disabled": false,
                 "disabled": false,
                 "__updated": 1444901673000,
                 "description": "this is test",
                 "created": 1401171574000,
                 "ratePlanMappingRequired": "true",
                 "updated": 1444901673000,
                 "gtaCode": "HY",
                 "__created": 1401171574000,
                 "derbyCode": "HYATT",
                 "cacheHour": "",
                 "taxCalRequired": "true",
                 "childAsAdultRequired": "true"
             }]
         }
         "success": true
     } 
     
     how to filter json path:
     |       XPath           |          JSONPath             |                     Result                                         | 
     | /store/book/author    |    $.store.book[*].author     |     the authors of all books in the store                          |
     | //author              |    $..author                  |     all authors                                                    | 
     | /store/*              |    $.store.*                  |     all things in store, which are some books and a red bicycle.   |
     | /store//price         |    $.store..price             |     the price of everything in the store.                          |
     | //book[3]             |    $..book[2]                 |     the third book                                                 |
     | //book[last()]        |    $..book[(@.length-1)]      |                                                                    |
     |                       |    $..book[-1:]               |     the last book in order.                                        |
     | //book[position()<3]  |    $..book[0,1]               |                                                                    |
     |                       |    $..book[:2]                |     the first two books                                            |
     | //book[isbn]          |    $..book[?(@.isbn)]         |     filter all books with isbn number                              |
     | //book[price<10]      |    $..book[?(@.price<10)]     |     filter all books cheapier than 10                              |
     | //*                   |    $..*                       |     all Elements in XML document. All members of JSON structure.   |
     
     jsonpath detail see:http://goessner.net/articles/JsonPath/
     '''
     jsonfile = CONTEXT.get_path(jsonfile)
     dlog.write(jsonfile)
     f = open(str(jsonfile))
     fs = f.read()
     f.close()
     j = json.loads(fs, encoding="utf-8", object_pairs_hook=OrderedDict)
     if type(patterns) is unicode:
         patterns = ast.literal_eval(patterns)
     if type(values) is unicode:
         values = ast.literal_eval(values)
     for pattern, v in zip(patterns, values):
         matches = jsonpath_rw.parse(pattern).find(j)
         for match in matches:
             j = json_ext.update_json(j, json_ext.get_path(match), v)
     nj = json.dumps(j, indent=4, ensure_ascii=False)
     dlog.write(nj)
     f = open(jsonfile, 'w')
     f.writelines(nj)
     f.close()