Friday, January 22, 2016

PHP cheat seet

PHP Array Functions 

 array_diff (arr1, arr2 …)
 array_filter (arr, function)
 array_flip (arr)
 array_intersect (arr1, arr2 …)
 array_merge (arr1, arr2 …)
 array_pop (arr)
 array_push (arr, var1, var2 …)
 array_reverse (arr)
 array_search (needle, arr)
 array_walk (arr, function)
 count (count)
 in_array (needle, haystack)

PHP String Functions

 crypt (str, salt)
 explode (sep, str)
 implode (glue, arr)
 nl2br (str)
 sprintf (frmt, args)
 strip_tags (str, allowed_tags)
 str_replace (search, replace, str)
 strpos (str, needle)
 strrev (str)
 strstr (str, needle)
 strtolower (str)
 strtoupper (str)
 substr (string, start, len)

PHP Filesystem Functions

 clearstatcache ()
 copy (source, dest)
 fclose (handle)
 fgets (handle, len)
 file (file)
 filemtime (file)
 filesize (file)
 file_exist (file)
 fopen (file, mode)
 fread (handle, len)
 fwrite (handle, str)
 readfile (file)

PHP Date and Time Fucntions

 checkdate (month, day, year)
 date (format, timestamp)
 getdate (timestamp)
 mktime (hr, min, sec, month, day, yr)
 strftime (formatstring, timestamp)
 strtotime (str)
 time ()

PHP Regular Expressions Functions

 ereg (pattern, str)
 split (pattern, str)
 ereg_replace (pattern, replace, str)
 preg_grep (patter, arr)
 preg_match (pattern, str)
 preg_match_all (pattern, str, srr)
 preg_replace (patter, replace, str)
 preg_split (patter, str)

Regular Expressions Syntax 

^
Start of string
$
End of string
.
Any single character
(a|b)
a or b
(…)
Group section
[abc]
In range (a, b or c)
[^abc]
Not in range
\s
White space
a?
Zero or one of a
a*
Zero or more of a
a*?
Zero or more, ungreedy
a+
One or more of a
a+?
One or more, ungreedy
a{3}
Exactly 3 of a
a{3,}
3 or more of a
a{,6}
Up to 6 of a
a{3,6}
3 to 6 of a
a{3,6}?
3 to 6 of a, ungreedy
\
Escape character
[:punct:]
Any punctu­ation symbol
[:space:]
Any space character
[:blank:]
Space or tab

Pattern Modifiers

g
Global match
i *
Case-i­nse­nsitive
m *
Multiple lines
s *
Treat string as single line
x *
Allow comments and whitespace in pattern
e *
Evaluate replac­ement
U *
Ungreedy pattern
* PCRE modifier

PHP fopen() modes

r
Read
r+
Read and write, prepend
w
Write, truncate
w+
Read and write, truncate
a
Write, append
a+
Read and write, append

PHP Date Formating

Y
4 digit year (2008)
y
2 digit year (08)
F
Long month (January)
M
Short month (Jan)
m
Month ⁴ (01 to 12)
n
Month (1 to 12)
D
Short day name (Mon)
l
Long day name (Monday) (lowercase L)
d
Day ⁴ (01 to 31)
j
Day (1 to 31)
h
12 Hour ⁴ (01 to 12)
g
12 Hour (1 to 12)
H
24 Hour ⁴ (00 to 23)
G
24 Hour (0 to 23)
i
Minutes ⁴ (00 to 59)
s
Seconds ⁴ (00 to 59)
w
Day of week ¹ (0 to 6)
z
Day of year (0 to 365)
W
Week of year ² (1 to 53)
t
Days in month (28 to 31)
a
am or pm
A
AM or PM
B
Swatch Internet Time (000 to 999)
S
Ordinal Suffix (st, nd, rd, th)
T
Timezone of machine (GMT)
Z
Timezone offset (seconds)
O
GMT offset (hours) (+0200)
I
Daylight saving (1 or 0)
L
Leap year (1 or 0)
U
Seconds since Epoch ³
c
ISO 8601 (PHP 5) (2008-­07-­31T­18:­30:­13+­01:00)
r
RFC 2822 (Thu, 31 Jul 2008 18:30:13 +0100)


¹ 0 is Sunday, 6 is Saturday.
² Week that overlaps two years belongs to year that contains most days of that week. Hence week number for 1st January of a given year can be 53 if week belongs to previous year. date(“W­”, mktime(0, 0, 0, 12, 8, $year)) always gives correct number of weeks in $year.
³ The Epoch is the 1st January 1970.
⁴ With leading zeroes
https://www.latesthackingnews.com/php-cheat-sheet-free-download/

C and C++ cheat sheet

C and C++ Cheat Sheet
libraries
#include input and output functions
#include string related functions
#include memory allocation, rand, and other functions
#include math functions
#include time related functions
functions
returnType functionName( input1Type input1Name, input2Type input2Name, …. )
{
// do something
return value; // value must be of type returnType
}
comments
// one line comments this is a C++ style one line comment
/* multiple line this is a traditional C style comment
block comment */
variable types
char holds a character, or a number from -128 to 127 (1 byte)
bool holds a boolean value, either true or false (1 byte)
int hold an integer (a positive or negative number with NO decimal, 4 bytes)
float holds a real number (a positive or negative number with a decimal, 4 bytes)
void no type, raw binary data
conditionals
A == B if A is equal to B, this is true; otherwise, it’s false
A != B if A is NOT equal to B, this is true; otherwise, it’s false
A < B if A is less than B, this is true; otherwise, it’s false A > B if A is greater B, this is true; otherwise, it’s false
A <= B if A is less than or equal to B, this is true; otherwise, it’s false A >= B if A is greater or equal to B, this is true; otherwise, it’s false
control flow 
if ( conditional )
{
// do something
}
if ( conditional )
{
// do something
}
else
{
// do something else
}
if ( conditional )
{
// do something
}
else if ( another_conditional )
{
// do something else
}
else
{
// do something as default
}
while ( conditional )
{
// do something
}
placing “break;” inside a while loop
breaks out of the loop
placing “continue;” inside a while
loop jumps to the start of the next
loop
for ( initialization; test; command )
{
// do something
}
“break;” and “continue;” can be
used within for loops as well with
identical effects
this is equivalent to:
initialization;
while( test )
{
// do something
command;
}

Uiautomatorviewer Tutorial

Identifying Android application (AUT) objects using Uiautomatorviewer to write automation scripts
To automate any android application using Appium, user needs to identify the objects in AUT (Application under test).
While executing automation scripts, Appium uses "Uiautomatorviewer" to identify different properties of the object and use the properties to identify the required object.

What is it?

"UIautomatorviewer" is a GUI tool to scan and analyze the UI components of an android application. With "UIautomatorviewer", you can inspect the UI of an android application in order to find out the hierarchy and view different properties (id, text…) of the element.

Where to get it?

"Uiautomatorviewer" is a part of the Android SDK manager and will be accessible once you install the sdk manager. Download and install Android SDK manger from here

How to get started?

Once Android SDK installed, navigate to link c:\users\\AppData\Local\Android\sdk\tools you'll notice a batch file with name 'uiautomatorviewer.bat'. Double click on it to launch "Uiautomatorviewer" GUI

How to use Uiautomatorviewer to find objects in my application

  1. Enable "developer" options on your device. Click here to know how to enable developer options on Android devices
  2. Connect your android device to PC via USB cable
  3. Select "Guru99" app from applications
  1. Click the 'Device screenshot' button to refresh the "Uiautomatorviewer" and to load the guru99 application GUI on "Uiautomatorviewer"
  1. After refresh is completed a screenshot of Guru99 application opens
  1. As you see in the above image, on the right side of the window there are 2 panels.
  • Upper panel contains node hierarchy the way the UI components are arranged and contained, clicking on the each node gives properties of UI element in the lower panel
  1. Select 'Quiz' button in the above image to view different properties (text, resource-id...)

How to use these properties to identify elements for automation

Well you cannot use the properties directly, each property has other names. Let's see how to use those properties values to work. Following attributes can be used to identify 'Quiz' button in Guru99 app.
  • text attribute can be used as "name"
  • resource-id attribute can be used as "id"
  • class attribute can be used as "className"
  • content-desc attribute can be used as "AccessibilityId"
    Along with above attributes we can write xpaths for object identification

Error one might encounter while using Uiautomatorviewer

  • I'm seeing the error- "No Android devices were detected by adb" as shown in below screen shot how I can resolve this
Solution: Make sure your device is connected to PC

APPIUM Tutorial For Beginners

APPIUM is a freely distributed open source mobile application UI testing framework.
Appium allows native, hybrid and web application testing and supports automation test on physical devices as well as on emulator or simulator both.
It offers cross-platform application testing i.e. single API works for both Android and iOS platform test scripts.
It has NO dependency on Mobile device OS. Because, APPIUM has framework or wrapper that translate Selenium Webdriver commands into UIAutomation (iOS) or UIAutomator (Android) commands depending on the device type not any OS type.
Appium supports all languages that have Selenium client libraries like- Java, Objective-C, JavaScript with node.js, PHP, Ruby, Python, C# etc.
In this tutorial we will learn about

APPIUM Design Concepts

  • Appium is an 'HTTP Server' written using Node.js platform and drives iOS and Android session using Webdriver JSON wire protocol. Hence, before initializing the Appium Server, Node.js must be pre-installed on the system.
  • When Appium is downloaded and installed, then a server is setup on our machine that exposes a REST API.
  • It receives connection and command request from the client and execute that command on mobile devices (Android / iOS).
  • It responds back with HTTP responses. Again, to execute this request, it uses the mobile test automation frameworks to drive the user interface of the apps. Framework like:-
    • Apple Instruments for iOS (Instruments are available only in Xcode 3.0 or later with OS X v10.5 and later)
    • Google UIAutomator for Android API level 16 or higher
    • Selendroid for Android API level 15 or less

Prerequisite to use APPIUM

  1. ANDROID SDK [Link]-
  2. JDK (Java Development Kit) [Link]
  3. TestNG [Link]
  4. Eclipse [Link]
  5. Selenium Server JAR [Link]
  6. Webdriver Language Binding Library [Link]
  7. APPIUM For Windows [Link]
  8. APK App Info On Google Play [Link]
  9. Node.js (Not Required - Whenever Appium server is installed, it by default comes with "Node.exe" & NPM. It's included in Current version of Appium.)

APPIUM Inspector

Similar to Selenium IDE record and playback tool, Appium has an 'Inspector' to record and Playback. It records and plays native application behavior by inspecting DOM and generates the test scripts in any desired language. However, currently there is no support for Appium Inspector for Microsoft Windows. In Windows, it launches the Appium Server but fails to inspect elements. However, UIAutomator viewer can be used as an option for Inspecting elements.
Steps to start with Appium Inspector on Mac machine:-
Step-1 Download and start your Appium server with the default IP Address 0.0.0.0 and the port 4725.
  1. Select the source file or .app files from local to test.
  2. Check the 'App Path' Checkbox to enable 'Choose' button.
Now, clicking on 'Choose' button will give the option to browse and select test file from the local drive.
Step 3- Start Simulator on Mac machine.
Step 4- Click 'Launch' button from top right corner, which enable a blue color icon. Again, click on this blue color icon, it will open the Appium inspector and Simulator with pre-selected application.
Step 5- Launching your Appium Inspector will show the element hierarchy in column-wise structure. Also, user can apply actions using buttons like Tap, Swipe etc.
Step 6- Click on 'Stop' button to stop recording.

APPIUM Installation on Windows

Step 1- Install Android SDK in your system.
a. Go to Control panel >> System and Security >> System and from the left panel click on 'Advance System Settings'. From 'System Properties' pop up, click on 'Advance' tab and then click on "Environment Variables" button.
b. Now, from 'Environment variables' pop up, 'double click on 'Path' and set ANDROID_HOME variable that point to your SDK directory. In the path append the whole SDK folder path.
e.g. - C:\User\ABC\Desktop\adt-bundled-windows-x86_64-20140321\sdk
Step 2- Start your Android emulator or any attach any Android device to your system (Make sure you have Android Debugging option enabled in your Android device. To check Debugging Option. Go to Device Settings >> Developer Options >> Check "Debugging Option").
Step 3- Open Command Prompt and navigate to your Android SDK's \platform-tools\ directory (E.g. D:\adt-bundle-windows-x86_64-20130514\sdk\platform-tools).
Step 4- Run 'adb devices' command. You can see your connected device listed in Command Prompt window. (In CMD write '>adb devices'- This command will list the connected emulator instances. E.g.: adb –s emulator-5554 install )
Step 5- Run 'adb start-server' command. It will start ADB server that will be used by Appium to send commands to your Android device.
Step 6- Now, navigate to Appium directory in your system and start Appium by clicking Appium.exe file.
Step 7- Do not alter the IP address or port number and click 'Launch' button. Your Appium console start at 127.0.0.1:4723 as shown in below.
Steps 8- Click on 'Start' button, Appium server started running on your system.

Your First APPIUM Test Case for Native Android App

Step 1) Download ADT eclipse plugin or download ADT bundled separately here
Step 2) Open Eclipse and Create a new Project >> Package >> Class
Step 3) Import Selenium library and TestNG inside that new project.
Step 4) Now Create a small test Program for 'Calculator.app' to sum two numbers.
Appium Server and Android Emulator from 'AVD Manager' and Click Run >> TestNG. Above program will run the 'Calculator.app' on selected emulator and Result displayed under Eclipse console using TestNG framework.

Limitations using APPIUM

  1. Appium does not support testing of Android Version lower than 4.2
  2. Limited support for hybrid app testing. eg: not possible to test the switching action of application from the web app to native and vice-versa.
  3. No support to run Appium Inspector on Microsoft Windows.

Common Encountered Errors and Troubleshooting Steps in Appium


Error
Troubleshooting Steps
1. error:- The following desired capabilities are required, but were not provided:device Name, platformName
1. Add desired capabilities: device Name, platformName in APPIUM script.
e.g:capabilities.setCapability("deviceName","Emulator"); capabilities.setCapability("platformName","Android");
2. error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
2. You probably need to set up SDK root directory path in system 'Environment Variables' in 'Path' column
3.error:org.openqa.selenium.SessionNotCreatedException: A new session could not be created.
3. You need to set a correct App path and restart the Appium server.
4. How to find DOM element or xPath in mobile application?
4. Use 'UIAutomatorviewer' to find DOM element for Android application.

Evil Twin attack

Evil Twin Attack is attack is frequently carried upon wireless access points with malicious intentions. This attack happens when...