Is there a way to get current imaplibrary instance from robot framework and pass to a separate python...












1















i am stuck in a testcase where i need to check that after performing an action, email is getting triggered, if yes then email has an attachment.



for the first action i am using Wait For Email keyword of robotframework's imaplibrary library. now for the attachment part since there is no keyword for this purpose i have written a separate python function to which i am passing email_index as parameter written by Wait For Email keyword. after that it should walk through the email and fetch attachment.



**robot file:**

${new_email}= Wait For Email sender=${sender_email} text=${expected_content} recipient=${recepient} timeout=70
${file} get_attachments ${new_email}


**python function**

import imaplib
import email

# m is the email index passed from wait for email keyword
def get_attachments(m):
if m.get_content_maintype() == 'multipart': #multipart messages only #getting below mentioned error in this line
for part in m.walk():

#find the attachment part
print part.get_content_maintype()
if part.get_content_maintype() == 'multipart': continue
if part.get('Content-Disposition') is None: continue

#save the attachment in the program directory
filename = part.get_filename()
return filename


now the problem is i am unable to share or pass the imaplibrary session created by robot framework to a custom python function. so am getting below error.




AttributeError: 'str' object has no attribute 'get_content_maintype'




i know there is a keyword get_library_instance() in Builtin library and am already using below code for getting selenium2libray driver instance.



def get_webdriver_instance():
se2lib = BuiltIn().get_library_instance('Selenium2Library')
return se2lib._current_browser()


is there any similar way to solve this issue for imaplibrary ? if not please suggest a way put for it.










share|improve this question


















  • 1





    As I don't have access to a working IMAP server I'm posting this as a comment. First of all, why do you think this isn't possible with the ImapLibrary as it clearly supports the multi-type content keywords you seek. Second, have you tr ied the BuiltIn().get_library_instance('ImapLibrary') and pass it to your function? It is a generic way of getting a library instance object to work with.

    – A. Kootstra
    Nov 23 '18 at 21:57











  • Your comment doesn't provide an answer to either question I asked. What you think you need may not be what you actually need. So, please re-read my first comment and respond to those questions.

    – A. Kootstra
    Nov 24 '18 at 7:45











  • as far as i know we can only check a type of email multipart/text type using imaplibray. there is no existing keyword to check and manipulate the attachments sent in an email. like my requirement is to read attachments & download attachments. and i also tried BuiltIn().get_library_instance('ImapLibrary') but somehow it didn't seemed helpful for my case

    – Rohit sai
    Nov 26 '18 at 18:57













  • however i achieved this by passing email_index to python function get_attachments

    – Rohit sai
    Nov 26 '18 at 19:03


















1















i am stuck in a testcase where i need to check that after performing an action, email is getting triggered, if yes then email has an attachment.



for the first action i am using Wait For Email keyword of robotframework's imaplibrary library. now for the attachment part since there is no keyword for this purpose i have written a separate python function to which i am passing email_index as parameter written by Wait For Email keyword. after that it should walk through the email and fetch attachment.



**robot file:**

${new_email}= Wait For Email sender=${sender_email} text=${expected_content} recipient=${recepient} timeout=70
${file} get_attachments ${new_email}


**python function**

import imaplib
import email

# m is the email index passed from wait for email keyword
def get_attachments(m):
if m.get_content_maintype() == 'multipart': #multipart messages only #getting below mentioned error in this line
for part in m.walk():

#find the attachment part
print part.get_content_maintype()
if part.get_content_maintype() == 'multipart': continue
if part.get('Content-Disposition') is None: continue

#save the attachment in the program directory
filename = part.get_filename()
return filename


now the problem is i am unable to share or pass the imaplibrary session created by robot framework to a custom python function. so am getting below error.




AttributeError: 'str' object has no attribute 'get_content_maintype'




i know there is a keyword get_library_instance() in Builtin library and am already using below code for getting selenium2libray driver instance.



def get_webdriver_instance():
se2lib = BuiltIn().get_library_instance('Selenium2Library')
return se2lib._current_browser()


is there any similar way to solve this issue for imaplibrary ? if not please suggest a way put for it.










share|improve this question


















  • 1





    As I don't have access to a working IMAP server I'm posting this as a comment. First of all, why do you think this isn't possible with the ImapLibrary as it clearly supports the multi-type content keywords you seek. Second, have you tr ied the BuiltIn().get_library_instance('ImapLibrary') and pass it to your function? It is a generic way of getting a library instance object to work with.

    – A. Kootstra
    Nov 23 '18 at 21:57











  • Your comment doesn't provide an answer to either question I asked. What you think you need may not be what you actually need. So, please re-read my first comment and respond to those questions.

    – A. Kootstra
    Nov 24 '18 at 7:45











  • as far as i know we can only check a type of email multipart/text type using imaplibray. there is no existing keyword to check and manipulate the attachments sent in an email. like my requirement is to read attachments & download attachments. and i also tried BuiltIn().get_library_instance('ImapLibrary') but somehow it didn't seemed helpful for my case

    – Rohit sai
    Nov 26 '18 at 18:57













  • however i achieved this by passing email_index to python function get_attachments

    – Rohit sai
    Nov 26 '18 at 19:03
















1












1








1








i am stuck in a testcase where i need to check that after performing an action, email is getting triggered, if yes then email has an attachment.



for the first action i am using Wait For Email keyword of robotframework's imaplibrary library. now for the attachment part since there is no keyword for this purpose i have written a separate python function to which i am passing email_index as parameter written by Wait For Email keyword. after that it should walk through the email and fetch attachment.



**robot file:**

${new_email}= Wait For Email sender=${sender_email} text=${expected_content} recipient=${recepient} timeout=70
${file} get_attachments ${new_email}


**python function**

import imaplib
import email

# m is the email index passed from wait for email keyword
def get_attachments(m):
if m.get_content_maintype() == 'multipart': #multipart messages only #getting below mentioned error in this line
for part in m.walk():

#find the attachment part
print part.get_content_maintype()
if part.get_content_maintype() == 'multipart': continue
if part.get('Content-Disposition') is None: continue

#save the attachment in the program directory
filename = part.get_filename()
return filename


now the problem is i am unable to share or pass the imaplibrary session created by robot framework to a custom python function. so am getting below error.




AttributeError: 'str' object has no attribute 'get_content_maintype'




i know there is a keyword get_library_instance() in Builtin library and am already using below code for getting selenium2libray driver instance.



def get_webdriver_instance():
se2lib = BuiltIn().get_library_instance('Selenium2Library')
return se2lib._current_browser()


is there any similar way to solve this issue for imaplibrary ? if not please suggest a way put for it.










share|improve this question














i am stuck in a testcase where i need to check that after performing an action, email is getting triggered, if yes then email has an attachment.



for the first action i am using Wait For Email keyword of robotframework's imaplibrary library. now for the attachment part since there is no keyword for this purpose i have written a separate python function to which i am passing email_index as parameter written by Wait For Email keyword. after that it should walk through the email and fetch attachment.



**robot file:**

${new_email}= Wait For Email sender=${sender_email} text=${expected_content} recipient=${recepient} timeout=70
${file} get_attachments ${new_email}


**python function**

import imaplib
import email

# m is the email index passed from wait for email keyword
def get_attachments(m):
if m.get_content_maintype() == 'multipart': #multipart messages only #getting below mentioned error in this line
for part in m.walk():

#find the attachment part
print part.get_content_maintype()
if part.get_content_maintype() == 'multipart': continue
if part.get('Content-Disposition') is None: continue

#save the attachment in the program directory
filename = part.get_filename()
return filename


now the problem is i am unable to share or pass the imaplibrary session created by robot framework to a custom python function. so am getting below error.




AttributeError: 'str' object has no attribute 'get_content_maintype'




i know there is a keyword get_library_instance() in Builtin library and am already using below code for getting selenium2libray driver instance.



def get_webdriver_instance():
se2lib = BuiltIn().get_library_instance('Selenium2Library')
return se2lib._current_browser()


is there any similar way to solve this issue for imaplibrary ? if not please suggest a way put for it.







python automation robotframework gmail-imap imaplib






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 20:28









Rohit saiRohit sai

495




495








  • 1





    As I don't have access to a working IMAP server I'm posting this as a comment. First of all, why do you think this isn't possible with the ImapLibrary as it clearly supports the multi-type content keywords you seek. Second, have you tr ied the BuiltIn().get_library_instance('ImapLibrary') and pass it to your function? It is a generic way of getting a library instance object to work with.

    – A. Kootstra
    Nov 23 '18 at 21:57











  • Your comment doesn't provide an answer to either question I asked. What you think you need may not be what you actually need. So, please re-read my first comment and respond to those questions.

    – A. Kootstra
    Nov 24 '18 at 7:45











  • as far as i know we can only check a type of email multipart/text type using imaplibray. there is no existing keyword to check and manipulate the attachments sent in an email. like my requirement is to read attachments & download attachments. and i also tried BuiltIn().get_library_instance('ImapLibrary') but somehow it didn't seemed helpful for my case

    – Rohit sai
    Nov 26 '18 at 18:57













  • however i achieved this by passing email_index to python function get_attachments

    – Rohit sai
    Nov 26 '18 at 19:03
















  • 1





    As I don't have access to a working IMAP server I'm posting this as a comment. First of all, why do you think this isn't possible with the ImapLibrary as it clearly supports the multi-type content keywords you seek. Second, have you tr ied the BuiltIn().get_library_instance('ImapLibrary') and pass it to your function? It is a generic way of getting a library instance object to work with.

    – A. Kootstra
    Nov 23 '18 at 21:57











  • Your comment doesn't provide an answer to either question I asked. What you think you need may not be what you actually need. So, please re-read my first comment and respond to those questions.

    – A. Kootstra
    Nov 24 '18 at 7:45











  • as far as i know we can only check a type of email multipart/text type using imaplibray. there is no existing keyword to check and manipulate the attachments sent in an email. like my requirement is to read attachments & download attachments. and i also tried BuiltIn().get_library_instance('ImapLibrary') but somehow it didn't seemed helpful for my case

    – Rohit sai
    Nov 26 '18 at 18:57













  • however i achieved this by passing email_index to python function get_attachments

    – Rohit sai
    Nov 26 '18 at 19:03










1




1





As I don't have access to a working IMAP server I'm posting this as a comment. First of all, why do you think this isn't possible with the ImapLibrary as it clearly supports the multi-type content keywords you seek. Second, have you tr ied the BuiltIn().get_library_instance('ImapLibrary') and pass it to your function? It is a generic way of getting a library instance object to work with.

– A. Kootstra
Nov 23 '18 at 21:57





As I don't have access to a working IMAP server I'm posting this as a comment. First of all, why do you think this isn't possible with the ImapLibrary as it clearly supports the multi-type content keywords you seek. Second, have you tr ied the BuiltIn().get_library_instance('ImapLibrary') and pass it to your function? It is a generic way of getting a library instance object to work with.

– A. Kootstra
Nov 23 '18 at 21:57













Your comment doesn't provide an answer to either question I asked. What you think you need may not be what you actually need. So, please re-read my first comment and respond to those questions.

– A. Kootstra
Nov 24 '18 at 7:45





Your comment doesn't provide an answer to either question I asked. What you think you need may not be what you actually need. So, please re-read my first comment and respond to those questions.

– A. Kootstra
Nov 24 '18 at 7:45













as far as i know we can only check a type of email multipart/text type using imaplibray. there is no existing keyword to check and manipulate the attachments sent in an email. like my requirement is to read attachments & download attachments. and i also tried BuiltIn().get_library_instance('ImapLibrary') but somehow it didn't seemed helpful for my case

– Rohit sai
Nov 26 '18 at 18:57







as far as i know we can only check a type of email multipart/text type using imaplibray. there is no existing keyword to check and manipulate the attachments sent in an email. like my requirement is to read attachments & download attachments. and i also tried BuiltIn().get_library_instance('ImapLibrary') but somehow it didn't seemed helpful for my case

– Rohit sai
Nov 26 '18 at 18:57















however i achieved this by passing email_index to python function get_attachments

– Rohit sai
Nov 26 '18 at 19:03







however i achieved this by passing email_index to python function get_attachments

– Rohit sai
Nov 26 '18 at 19:03














1 Answer
1






active

oldest

votes


















1














i was not able to use the instance of imaplibrary for this purpose but found another way for achieving this. the main purpose for this question is to see how to handle gmail attachments related cases in robot framework(like check/read/downloading the attachments). below is code for it. for this below is a small custom function for achieving the same.



**robot file:**
Check Mail
${new_email}= Wait For Email sender=${sender_email} text=${expected_content} recipient=${recepient} timeout=70
${file} get_attachments ${new_email}
log many ${file}


**python function**

#index is the email index passed from wait for email keyword
def get_attachments(index):
files=
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('email', 'password')
mail.select('inbox')

result, data = mail.uid('fetch',index, '(RFC822)')
m = email.message_from_string(data[0][1])
if m.get_content_maintype() == 'multipart':
for part in m.walk():
#logger.console(part)

#find the attachment part
if part.get_content_maintype() == 'multipart': continue
if part.get('Content-Disposition') is None: continue

#save the attachment in the program directory
filename = part.get_filename()
files.append(filename)
fp = open(filename, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
return files





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452613%2fis-there-a-way-to-get-current-imaplibrary-instance-from-robot-framework-and-pass%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    i was not able to use the instance of imaplibrary for this purpose but found another way for achieving this. the main purpose for this question is to see how to handle gmail attachments related cases in robot framework(like check/read/downloading the attachments). below is code for it. for this below is a small custom function for achieving the same.



    **robot file:**
    Check Mail
    ${new_email}= Wait For Email sender=${sender_email} text=${expected_content} recipient=${recepient} timeout=70
    ${file} get_attachments ${new_email}
    log many ${file}


    **python function**

    #index is the email index passed from wait for email keyword
    def get_attachments(index):
    files=
    mail = imaplib.IMAP4_SSL('imap.gmail.com')
    mail.login('email', 'password')
    mail.select('inbox')

    result, data = mail.uid('fetch',index, '(RFC822)')
    m = email.message_from_string(data[0][1])
    if m.get_content_maintype() == 'multipart':
    for part in m.walk():
    #logger.console(part)

    #find the attachment part
    if part.get_content_maintype() == 'multipart': continue
    if part.get('Content-Disposition') is None: continue

    #save the attachment in the program directory
    filename = part.get_filename()
    files.append(filename)
    fp = open(filename, 'wb')
    fp.write(part.get_payload(decode=True))
    fp.close()
    return files





    share|improve this answer






























      1














      i was not able to use the instance of imaplibrary for this purpose but found another way for achieving this. the main purpose for this question is to see how to handle gmail attachments related cases in robot framework(like check/read/downloading the attachments). below is code for it. for this below is a small custom function for achieving the same.



      **robot file:**
      Check Mail
      ${new_email}= Wait For Email sender=${sender_email} text=${expected_content} recipient=${recepient} timeout=70
      ${file} get_attachments ${new_email}
      log many ${file}


      **python function**

      #index is the email index passed from wait for email keyword
      def get_attachments(index):
      files=
      mail = imaplib.IMAP4_SSL('imap.gmail.com')
      mail.login('email', 'password')
      mail.select('inbox')

      result, data = mail.uid('fetch',index, '(RFC822)')
      m = email.message_from_string(data[0][1])
      if m.get_content_maintype() == 'multipart':
      for part in m.walk():
      #logger.console(part)

      #find the attachment part
      if part.get_content_maintype() == 'multipart': continue
      if part.get('Content-Disposition') is None: continue

      #save the attachment in the program directory
      filename = part.get_filename()
      files.append(filename)
      fp = open(filename, 'wb')
      fp.write(part.get_payload(decode=True))
      fp.close()
      return files





      share|improve this answer




























        1












        1








        1







        i was not able to use the instance of imaplibrary for this purpose but found another way for achieving this. the main purpose for this question is to see how to handle gmail attachments related cases in robot framework(like check/read/downloading the attachments). below is code for it. for this below is a small custom function for achieving the same.



        **robot file:**
        Check Mail
        ${new_email}= Wait For Email sender=${sender_email} text=${expected_content} recipient=${recepient} timeout=70
        ${file} get_attachments ${new_email}
        log many ${file}


        **python function**

        #index is the email index passed from wait for email keyword
        def get_attachments(index):
        files=
        mail = imaplib.IMAP4_SSL('imap.gmail.com')
        mail.login('email', 'password')
        mail.select('inbox')

        result, data = mail.uid('fetch',index, '(RFC822)')
        m = email.message_from_string(data[0][1])
        if m.get_content_maintype() == 'multipart':
        for part in m.walk():
        #logger.console(part)

        #find the attachment part
        if part.get_content_maintype() == 'multipart': continue
        if part.get('Content-Disposition') is None: continue

        #save the attachment in the program directory
        filename = part.get_filename()
        files.append(filename)
        fp = open(filename, 'wb')
        fp.write(part.get_payload(decode=True))
        fp.close()
        return files





        share|improve this answer















        i was not able to use the instance of imaplibrary for this purpose but found another way for achieving this. the main purpose for this question is to see how to handle gmail attachments related cases in robot framework(like check/read/downloading the attachments). below is code for it. for this below is a small custom function for achieving the same.



        **robot file:**
        Check Mail
        ${new_email}= Wait For Email sender=${sender_email} text=${expected_content} recipient=${recepient} timeout=70
        ${file} get_attachments ${new_email}
        log many ${file}


        **python function**

        #index is the email index passed from wait for email keyword
        def get_attachments(index):
        files=
        mail = imaplib.IMAP4_SSL('imap.gmail.com')
        mail.login('email', 'password')
        mail.select('inbox')

        result, data = mail.uid('fetch',index, '(RFC822)')
        m = email.message_from_string(data[0][1])
        if m.get_content_maintype() == 'multipart':
        for part in m.walk():
        #logger.console(part)

        #find the attachment part
        if part.get_content_maintype() == 'multipart': continue
        if part.get('Content-Disposition') is None: continue

        #save the attachment in the program directory
        filename = part.get_filename()
        files.append(filename)
        fp = open(filename, 'wb')
        fp.write(part.get_payload(decode=True))
        fp.close()
        return files






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 15 at 21:27

























        answered Nov 26 '18 at 19:17









        Rohit saiRohit sai

        495




        495
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452613%2fis-there-a-way-to-get-current-imaplibrary-instance-from-robot-framework-and-pass%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen