• +43 660 1453541
  • contact@germaniumhq.com

Writing an E-Mail From GMail Using Germanium




Writing an E-Mail From GMail Using Germanium

This short tutorial will show you how you can write an e-mail using the Google’s GMail, by automating the browser itself.

What We Want To Achieve

The end result should look like:

Germanium Send Mail Demo GIF Animaton

And, like in the recording, it needs to be fully automated. All you need installed beside Python (2 or 3, both are supported) is Germanium:

1
pip install germanium

Implementation

We’ll start simple by just including the required germanium API, and by opening the browser:

1
2
3
4
from germanium.static import *

open_browser("chrome")
get_germanium().maximize_window()

We obviously need to go to the GMail website:

1
go_to('http://mail.google.com')

Then we go and fill in our credentials. In order not to have them available as plain/text in the test code, we will use some environment variables. This is great because we can reuse this script also on our CI machine, or even Germanium docker containers.

1
2
3
4
5
6
7
8
9
10
from os import env

type_keys(os.environ['GMAIL_USER'],
InputText('Enter your email'))
click(Button('Next'))

wait(InputText('Password'))
type_keys(os.environ['GMAIL_PASSWORD'],
InputText('Password'))
click(Button('Sign in'))

Now that we’ve initiated the click, we need to wait for the page to load:

1
wait(GmailButton('COMPOSE'))

Wait, are we waiting on a custom button selector? Yes. We’ll define this selector as this function:

1
2
3
4
def GmailButton(title):
return Element("div",
exact_text=title,
exact_attributes={'role': 'button'})

Then let’s compose an e-mail to ourselves. Spam is bad, spammers are horrible people, so we’re not going to spam anyone.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
click(GmailButton('COMPOSE'))

wait(Text('To').below(Text('New Message')))
type_keys(os.environ['GMAIL_USER'] + "<cr>")

click(InputText('Subject').below(Text('New Message')))
type_keys('Germanium is Awesome!')

# we're going to TAB change into the message body
type_keys('<tab>')

type_keys('Germanium <c-i>is<c-i> <c-b>Awesome<c-b>!')

click(GmailButton('Send'))

Now that we sent the e-mail we should wait for it to be sent:

1
wait(Text('Your message has been sent'))

Then just the cleanup:

1
close_browser()

Download the full script from here.