Python Selenium Tips

Hello everyone, I’m Ceyhun. Today i will share some tips regarding preparing and using Selenium scripts with Python.

-Use page object model, its very important to start your project with it.. Make some demo project or start with basic writing and show difference to your team.
-Use unittest module or something similar to organize your cases. It makes easier to run and manage.
-Use driver.quit() not close if you gonna run many cases on a server.
-Use expected conditions very often or always.
-Use try except functions at your setup. Lets say you have 30 cases requires a setup before run and you put your driver.quit function inside the teardown function. What will happen if setup fails? You will have 30 chrome window open. Sometimes you can take some actions to make your scripts look like a poem but design them well.
-Follow PEP8 guides for better code quality. You are probably in a QA department. Well improve quality of your own work.
-Use power of the unittest! Expected Failure, Skip ext. You can make your tests easier if you can check its build in functions.
-Take screenshots with teardown, it makes your life easier if you gather screenshots once your script fails, do it.
-Prepare your base for different platforms. Python can provide what os and platform you are working on. Arrange your settings file to work with different platform without additional settings by end users(your team members).
-Maintain and run your cases. If you only write but don’t run your cases your team members won’t understand its effect. Focus on reporting and maintaining your cases, show your business partners the value of automation, if you don’t do that they can request to stop automation work since it takes more time to write scripts. Schedule may be important but dont give up from quality that easily.

Cleaning TMP Folder for Jenkins

I’m using jenkins with alot of plugins, also running my selenium cases on same machine so as you guess we have very big tmp folder after each day.

Do fix this issue, i added a cron job, used root access since some files requires it before deleting.
Its not safe and i suggest using another jenkins plugin for task but who cares right? 🙂

sudo crontab -e

30 2 * * * rm -r /tmp/*

There you go.

Graphite Installation Problems

I spent some time to install graphite then decided to not use it since it requires more effort and i have to reinstall again to another machine. Here some notes in case you face some problems:

Carefull to uncomment your settings file’s required field, most importantly db side.
There is no manage.py file anymore so use these commends if you need them:

sudo django-admin.py migrate  –pythonpath /opt/graphite/webapp –settings graphite.settings –run-syncdb
sudo django-admin.py makemigrations  –pythonpath /opt/graphite/webapp –settings graphite.settings
sudo django-admin.py collectstatic –noinput –pythonpath /opt/graphite/webapp –settings graphite.settings



For permission problems at ubuntu change own. of folder to apache2 user like this:

sudo chown -R www-data:www-data /opt/graphite
 
If you are using new Django version, you need to edit urls you also need to edit setting file in order to fix template problem.
 
If i continue to install/use graphite i may add more tips but these are all for now, good luck!

 

Jenkins Python Selenium Installation

Hello everyone i was facing some problems so i had to setup my jenkins server again so here are steps “after” jenkins installation, you can check it below, first install git.

apt-get install git

Setup your git repo and settings in project page. You should not see any error message when you add credentials. Git repo name like this https://github.com/mhangman/Gralab.git

apt-get install Xvfb

Arrange your resolution and display name offset which you should set as 1
Jenkins Settings – > Global Tool Settings -> Xvfb installation -> Write adress /usr/bin

apt-get install python-pip
sudo pip install selenium
apt-get install python-mysqldb
apt-get install firefox

You also need to arrange your slack or mail settings, right now i’m using mail but i will soon add it. For mail setting check your port for ssl it was 465 in my case.

Python Selenium Text Check

Hi,

Below code may help you to check source with input text:

    def is_text_present(self, text):
        if str(text) in self.driver.page_source:
            print “Found this text %s” % text
        else:
            Exception()
            print “ERROR: Could not find text.”

Firefox 47 Linux Downgrade

We faced some problems between firefox 47 and selenium thats why we decided to downgrade to version 46 on our development and jenkins platforms.

 Here simple guide for downgrading it. Sadly ubuntu only support latest version of package (version 47) and version 28 so we will do it manually.

Remove existing one but don’t delete rest:

sudo apt-get remove firefox

get version 46 for 64bit system

wget https://ftp.mozilla.org/pub/firefox/releases/46.0/linux-x86_64/en-US/firefox-46.0.tar.bz2

tar -xjf firefox-46.0.tar.bz2
sudo rm -rf /opt/firefox
sudo mv firefox /opt/firefox46
sudo mv /usr/bin/firefox /usr/bin/firefoxold
sudo ln -s /opt/firefox46/firefox /usr/bin/firefox

Thats all, after these your selenium script can work like before. I tried marionette driver but it doesnt work for me so downgrade was better option.

In case if you installed v46 as package and want to hold its upgrade use this command

sudo apt-mark hold firefox

to unhold firefox package you can use this command

sudo apt-mark unhold firefox
sudo apt-get upgrade

Best Regards