[ selenium ] with TestNG and WebDriver

1) TestNG Eclipse Plugin Installation

  1. In eclipse, Help > Install New Software …
  2. Add Repository http://beust.com/eclipse and install

ref : http://toolsqa.com/selenium-webdriver/install-testng/

2) Create TestNG Class

  1. Right click at package, TestNG > create TestNG class
  2. Fill-in classname, we can also check the @BeforeMethod, @AfterMethod annotation
  3. Run : Run As > TestNG Test

ref : http://toolsqa.com/selenium-webdriver/testng-testcase/

3) Add Selenium-java and testNG to Library

selenium-java-testng.png

NOTE : All JARs inside Selenium-java.zip need to be added, otherwise we will got below error.

FAILED CONFIGURATION: @BeforeClass setUpFAILED CONFIGURATION: @BeforeClass setUpjava.lang.NoClassDefFoundError: com/google/common/base/Function at my.my.test.NewTest.setUp(NewTest.java:28)

 

Common Run Error

java.lang.UnsupportedClassVersionError: org/openqa/selenium/NoAlertPresentException : Unsupported major.minor version 52.0

Root Cause

The version of Selenium you have requires Java 8.
You are not running this on Java 8.

ref : https://www.reddit.com/r/learnjava/comments/5jzbjx/javalangunsupportedclassversionerror/

ChromeDriver common error

The path to the driver executable must be set by the webdriver.chrome.driver system property

Solution 

System.setProperty("webdriver.chrome.driver", "C:/Users/phongsathorn.e/Downloads/chromedriver_win32/chromedriver.exe");

driver = new ChromeDriver();

ref : https://stackoverflow.com/questions/44476647/getting-the-path-to-the-driver-executable-must-be-set-by-the-webdriver-chrome-d

Download ChromeDriver.exe at https://sites.google.com/a/chromium.org/chromedriver/downloads

Ref : Download previous version of selenium

http://selenium-release.storage.googleapis.com/index.html

 

[ rails ] ActiveMailer send email to Gmail using SMTP and TLS

Mailer

app/mailers/user_mailer.rb

class UserMailer < ApplicationMailer
helper :application

def generate(recipients:, params2:)
attachments["#{Date.yesterday}.csv"] = "x,y,z"
mail(
to: recipients,
subject: "#{name}--#{Date.today}",
body: 'Report is attached'
)
end

end

 

Send email

UserMailer.generate(
param1: 'whatever',
param2: 'whatever'
).deliver_now!

NOTE*** : the ! after deliver_now will force rails to throw error.

 

SMTP setting

config/environment.rb

ActionMailer::Base.smtp_settings = {
user_name: 'phong.ek@gmail.com',
password: 'whatever',
domain: 'gmail.com',
address: 'smtp.gmail.com',
port: 587,
authentication: :plain,
enable_starttls_auto: true
}

 

Common error and solution

Error#1

EOFError: end of file reached
from /home/pong/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/net/protocol.rb:153:in `read_nonblock'

Root cause#1

One of the root cause is using SSL port 465 with TLS.

NOTE : According to https://support.google.com/a/answer/176600?hl=en, port 587 must be TLS, port 465 should be SSL.

 

Error#2

Username and Password are not accepted

Root cause#2

See https://dsin.wordpress.com/2017/09/28/gmail-unable-to-send-email/

 

Useful params in config/development.rb

The above script log the error even the environment configuration is below.

config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_deliveries = false

 

Mail Catcher
Another interesting rails mail server is https://mailcatcher.me/.

[ rails ] AbstractController callback

order

prepend_around_action
prepend_before_action
before_action
append_before_action
around_action
append_around_action

Rendered layouts/application.html.erb (0.5ms)
Rendered home/index.html.erb (0.5ms)

append_after_action
after_action
prepend_after_action

ref : http://hollandaiseparty.com/order-of-abstractcontrollercallbacks/

[ firefox ] Selenium IDE

Installation

https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/

or link from http://www.seleniumhq.org/download/

Feature

  • Record and playback.
  • Export to selenium WebDriver

* JUnit 4 / WebDriver Backed
* Java / TestNG / WebDriver
* Java / JUnit 4 / WebDriver Backed
* Java / JUnit 4 / Remote Control
* Java / JUnit 3 / Remote Control
* Java / TestNG / Remote Control

* C# / NUnit / WebDriver

* C# / NUnit / Remote Control

* Python 2 / unittest / WebDriver

* Python 2 / unittest / Remote Control

* Ruby / RSpec / WebDriver

* Ruby / Test::Unit / WebDriver

* Ruby / RSpec / Remote Control

* Ruby / Test::Unit / Remote Control

How to use if-else in IDE

Command |Target | value
store | true | var1
if | storedVars.var1
echo | I am in the if
else
echo | I am in the else
endIf

ref : https://stackoverflow.com/questions/36883304/how-to-use-if-else-statement-in-selenium-ide

 

 

[ google chrome ] notification setting

Setup block & allow

chrome://settings/content/notifications

Notification

Ask for subscription

// request permission on page load
document.addEventListener('DOMContentLoaded', function () {
 if (!Notification) {
 alert('Desktop notifications not available in your browser. Try Chromium.');
 return;
 }

if (Notification.permission !== "granted")
 Notification.requestPermission();
});

sending notification

function notifyMe() {
  if (Notification.permission !== "granted")
    Notification.requestPermission();
  else {
    var notification = new Notification('Notification title', {
      icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
      body: "Hey there! You've been notified!",
    });

    notification.onclick = function () {
      window.open("http://stackoverflow.com/a/13328397/1269037");
    };
  }
}

ref : https://stackoverflow.com/questions/2271156/chrome-desktop-notification-example