Monday, 25 September 2017

Mouse Move Software (A simple JAVA code functionality) : Keep your computer awake without touching your mouse

Hi Everyone ,

I have a piece of a Java code very useful for those who can't change their system sleep settings to NEVER
Sharing you an easy work around ! Hope it helps ..


What is the below code doing ?
Answer: Below code is basically changing the cursor location to and fro in a continuous loop. In between every iteration in a loop there is a timedelay. Timedelay is a user input which is required at the starting of this program. As per the Timedelays ( ms ) provided from user, code will wait for some time in between of every iteration. Lets say user provided 3000 as an input , so code will wait for 3 seconds and re perform the mouse move task

Code Hierarchy :
Its a simple code , MouseMoveclass field timedelay is made private ( I dont want any other class to access or change the variable )

Created an explicit Constructor which has arguments ( This argument will inturn initiate private field timedelay ). Constructors in java are used to initiate the fields and methods , and so am i doing ...

Created a method mousemovefunc() which includes:
Robot class instance --- used for mouse actions

Finally a main function which includes :
Scanner class instance to get user value. This user value is used while class instance is made


Code :

package mousemovepackage;

import java.awt.MouseInfo;
import java.awt.Robot;
import java.util.Scanner;

public class MouseMoveclass {

private int timedelay;

public MouseMoveclass(int val){
this.timedelay = val;
System.out.println("Thanks ! Timedelay updated to "+ String.valueOf(timedelay) + "ms");
}
public void mousemovefunc() throws Exception{
Robot myinstance = new Robot();
while(true){
myinstance.delay(timedelay);
java.awt.Point pobj = MouseInfo.getPointerInfo().getLocation();
myinstance.mouseMove(pobj.x+2, pobj.y);
myinstance.delay(2000);
myinstance.mouseMove(pobj.x-2, pobj.y);
}
}
public static void main(String[] args) throws Exception {
System.out.println("Please enter the time delay(ms): ");
Scanner sc = new Scanner(System.in);
int value = sc.nextInt();
MouseMoveclass Mouseinstance = new MouseMoveclass(value);
Mouseinstance.mousemovefunc();
sc.close();
}
}

How to Create a Runnable Jar and create a One click Solution which can run one any machine ?
Follow below Steps :
Right click on the project > Click on Export :




















Click on Runnable JAR file > Next


























Select Launch configuration and Export destination and hit Finish


























Finally we will get a jar file of 2KB approx . Place this file in a folder lets say "MouseMove"
Create a bat file ( Lets say setup.bat ) 
Edit this bat file with below code :


@Echo Off

Set "JV="
For /F "Tokens=3" %%A In ('java -version 2^>^&1') Do If Not Defined JV Set "JV=%%~A"
If /I "%JV%"=="not" (Echo Java is not installed) Else Echo Java Version "%JV%"

TIMEOUT 5

java -jar C:\MouseMove\MouseMoveSoftware.jar
Pause

Code is almost done , We just need to double click setup.bat in any machine and that's it !!!

Happy Coding !!!!

Wednesday, 24 May 2017

Esspresso Custom Matchers

Esspresso Custom Matchers


private static Matcher<View> childAtPosition(
       
final Matcher<View> parentMatcher, final int position) {

   
return new TypeSafeMatcher<View>() {
       
@Override
       
public void describeTo(Description description) {
            description.appendText(
"Child at position " + position + " in parent ");
           
parentMatcher.describeTo(description);
        }

       
@Override
       
public boolean matchesSafely(View view) {
            ViewParent parent = view.getParent();
           
return parent instanceof ViewGroup && parentMatcher.matches(parent)
                    && view.equals(((ViewGroup) parent).getChildAt(
position));
        }
    };
}


Points to remember:

1.       ChildAtPosition is a custom Matcher view created above
2.       Custom view matcher requires two attributes which will be verified
a.       parentMatcher ( resource-id of parent.  Eg..  R.id.YourIDName)
b.      Child Position (which is the Index position of child )
3.       TypeSafeMatcher.matchesSafely() simply returns Boolean value if the parent is instanceof ViewGroup
4.       TypeSafeMatcher.describeTo() is simply used for debugging in console

How to use childAtPosition Matcher

Sample view from UIAutomatorViewer :



Child ID : miniBtnFour : Index : 3
Parent_One ID : fragmentCountingMenu : 0
Parent of Parent_One ID : nav_menu_fragment : 2

Code to Integrate:


ViewInteraction button77 = onView(
        allOf(withId(R.id.miniBtnFour),withText("Submit"),
                childAtPosition(
                        childAtPosition(
                               withId(R.id.nav_menu_fragment),0),
                        3),
                isDisplayed()));
button77.check(matches(withText("Submit")));
button77.perform(click());

Wednesday, 5 April 2017

Python Scripting - Automation

Python Unittest | Pyunit


Python Unit Test Module or PyUnit is a unit test framework similar to Junit .The unittest module provides classes that make it easy to support these qualities for a set of tests. Helps in creating easy Automation and handling big codes !

How to install :
Pip install unittest   ---- Step 1

Unit Test Features :
  • supports test automation
  • sharing of setup and shutdown code for tests
  • aggregation of tests into collections
  • independence of the tests from the reporting framework



Basic Functions in  UnitTest :

test fixture
test fixture represents the preparation needed to perform one or more tests, and any associate cleanup actions. This may involve, for example, creating temporary or proxy databases, directories, or starting a server process.
test case
test case is the smallest unit of testing. It checks for a specific response to a particular set of inputs. unittest provides a base class, TestCase, which may be used to create new test cases.
test suite
test suite is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together.
test runner
test runner is a component which orchestrates the execution of tests and provides the outcome to the user. The runner may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.

Sample code :

------------------------------------------------Importing Libraries------------------------------------
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from pynput.keyboard import Key, Controller

def Keyboard_press_release():
    keyboard.press(Key.ctrl)
    keyboard.press(Key.tab)
    keyboard.release(Key.ctrl)
    keyboard.release(Key.tab)        

 ------------------------------------------Main Class --------------------------------------------------------------

class AdminPortalWorkflowTest(unittest.TestCase):

------------------------------------------Test case 1 --------------------------------------------------------------
    def test_Launch(self):
        self.driver = webdriver.Chrome('C:\Python27\Scripts\chromedriver.exe')       
        global keyboard
        keyboard = Controller()
        driver = self.driver
        driver.get("https://www.google.com")
        time.sleep(5) 
        Keyboard_press_release()
----------------------------------------Test case 2 ----------------------------------------------------------------                 
    def test_Check(self):
        print "func starts ..."     

----------------Created a Suite which combines Testcase 1and 2 and returns a List -----------------
           
def suite():
    suite = unittest.TestSuite()
    suite.addTest(AdminPortalWorkflowTest('test_Launch'))
    suite.addTest(AdminPortalWorkflowTest('test_Check'))
    return suite                                   

-----------------------------------------Main Function ------------------------------------------------------------
if __name__ == '__main__':

--------------------------------- Calling Suite which runs all test  --------------------------------------------
unittest.TextTestRunner(verbosity=2).run(suite())


Results :