UNPKG

testing-fm1

Version:

Creates folders and files required for testing in Devicefarm

373 lines (286 loc) 11.8 kB
#! /usr/bin/env node var fs = require("fs"); var path = require("path"); var program = require("commander"); function run(name, options) { var dir = path.resolve(name); var babelrc = path.resolve(dir,".babelrc"); var jshintrc = path.resolve(dir,".jshintrc"); var jestConfig = path.resolve(dir,"jest.config.js"); var packageJSON = path.resolve(dir,"package.json"); var appiumTesting = path.resolve(dir+"/__tests__/","appium_testing.js"); var babelrcFile = `{ "presets": ["module:metro-react-native-babel-preset"] }`; var jshintrcFile = `{ "enable":false, "ajv":false, "jasmin":false, "jest": false } `; var jestconfigFile = `module.exports = { // All imported modules in your tests should be mocked automatically // automock: false, // Stop running tests after \`n\` failures // bail: 0, // Respect "browser" field in package.json when resolving modules // browser: false, // The directory where Jest should store its cached dependency information // cacheDirectory: "C:\\Users\\YADAMS\\AppData\\Local\\Temp\\jest", // Automatically clear mock calls and instances between every test clearMocks: true, // Indicates whether the coverage information should be collected while executing the test // collectCoverage: false, // An array of glob patterns indicating a set of files for which coverage information should be collected collectCoverageFrom: ["<rootDir>/__tests__/"], // The directory where Jest should output its coverage files coverageDirectory: "coverage", // An array of regexp pattern strings used to skip coverage collection // coveragePathIgnorePatterns: [ // "\\\\node_modules\\\\" // ], // A list of reporter names that Jest uses when writing coverage reports // coverageReporters: [ // "json", // "text", // "lcov", // "clover" // ], // An object that configures minimum threshold enforcement for coverage results // coverageThreshold: null, // A path to a custom dependency extractor // dependencyExtractor: null, // Make calling deprecated APIs throw helpful error messages // errorOnDeprecated: false, // Force coverage collection from ignored files using an array of glob patterns // forceCoverageMatch: [], // A path to a module which exports an async function that is triggered once before all test suites // globalSetup: null, // A path to a module which exports an async function that is triggered once after all test suites // globalTeardown: null, // A set of global variables that need to be available in all test environments // globals: {}, // An array of directory names to be searched recursively up from the requiring module's location moduleDirectories: [ "node_modules" ], // An array of file extensions your modules use moduleFileExtensions: [ "js", "json", "jsx", "ts", "tsx", "node" ], // A map from regular expressions to module names that allow to stub out resources with a single module // moduleNameMapper: {}, // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader modulePathIgnorePatterns: ["<rootDir>/node_modules/"], modulePaths:["<rootDir>"], // Activates notifications for test results // notify: false, // An enum that specifies notification mode. Requires { notify: true } // notifyMode: "failure-change", // A preset that is used as a base for Jest's configuration // preset: null, // Run tests from one or more projects // projects: null, // Use this configuration option to add custom reporters to Jest // reporters: undefined, // Automatically reset mock state between every test // resetMocks: false, // Reset the module registry before running each individual test // resetModules: false, // A path to a custom resolver // resolver: null, // Automatically restore mock state between every test // restoreMocks: false, // The root directory that Jest should scan for tests and modules within rootDir: "", // A list of paths to directories that Jest should use to search for files in // roots: [ // "<rootDir>/__tests__/","<rootDir>","<rootDir>/__tests__/specs/" // ], // Allows you to use a custom runner instead of Jest's default test runner // runner: "jest-runner", // The paths to modules that run some code to configure or set up the testing environment before each test // setupFiles: [], // A list of paths to modules that run some code to configure or set up the testing framework before each test // setupFilesAfterEnv: [], // A list of paths to snapshot serializer modules Jest should use for snapshot testing // snapshotSerializers: [], // The test environment that will be used for testing testEnvironment: "node", // Options that will be passed to the testEnvironment // testEnvironmentOptions: {}, // Adds a location field to test results // testLocationInResults: false, // The glob patterns Jest uses to detect test files // testMatch: [ // "**/__tests__/**/*.[jt]s?(x)", // "**/?(*.)+(spec|test).[tj]s?(x)" // ], // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped testPathIgnorePatterns: [ "<rootDir>/node_modules/" ], // The regexp pattern or array of patterns that Jest uses to detect test files // testRegex: ["(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$"], testRegex: "/__tests__/[^.].*js$", haste: { "providesModuleNodeModules": ["nodemodules"] }, // This option allows the use of a custom results processor // testResultsProcessor: null, // This option allows use of a custom test runner // testRunner: "jasmine2", // This option sets the URL for the jsdom environment. It is reflected in properties such as location.href // testURL: "http://localhost", // Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout" // timers: "real", // A map from regular expressions to paths to transformers // transform: { '^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js' }, // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation // transformIgnorePatterns: [ // "\\\\node_modules\\\\" // ], // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them // unmockedModulePathPatterns: undefined, // Indicates whether each individual test should be reported during the run // verbose: null, // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode // watchPathIgnorePatterns: [], // Whether to use watchman for file crawling // watchman: true, }; `; var packageJsonFile = ` { "name": "devicefarmtest", "version": "1.0.0", "description": "time tracking test cases", "main": "index.js", "scripts": { "test": "jest --config=jest.config.js", "appium": "appium", "test:watch": "jest --watch", "test:coverage": "jest --coverage --colors" }, "author": "suresh", "license": "ISC", "dependencies": { "babel-jest": "^24.8.0", "jest": "^24.8.0", "jshint": "^2.10.2", "wd": "^1.11.2" }, "devDependencies": { "@babel/core": "^7.4.4", "@babel/runtime": "^7.4.4", "appium": "^1.13.0", "appium-doctor": "^1.10.0", "babel-jest": "^24.8.0", "jest": "^24.8.0", "jshint": "^2.10.2", "metro-react-native-babel-preset": "^0.54.1", "wd": "^1.11.2" }, "bundledDependencies": [ "wd", "jest", "appium", "jshint", "metro-react-native-babel-preset" ] } `; var appiumTestingFile = `/* Name of the project: This File is used to write test cases, few are already written for you. Add your own test cases and start testing end to end test cases. */ import wd from 'wd'; jasmine.DEFAULT_TIMEOUT_INTERVAL = 120000; const PORT = 4723; const config = { // platformName: 'Android', // autoGrantPermissions :true, // allowTouchIdEnroll:true, // clearSystemFiles:true, // app: 'C:/Users/YADAMS/Documents/TysonProjects/ReactNativeApps/time-tracking/android/app/build/outputs/apk/debug/app-debug.apk', // relative to root of project // deviceName: 'Nexus_S_API_27' }; const driver = wd.promiseChainRemote('localhost', PORT); //Mostly used function beforeAll(async () => { await driver.init(config); await driver.sleep(20000); // wait for app to load }); /* In the beginning of running test cases, i would like to know about current driver status and device time(test started time). */ test('Server current status',async()=>{ let status = await driver.status(); let time = await driver.getDeviceTime(); console.log('Test started time', time); console.log('Server version - status' ,status); // Will get to know server version. }); /** * Testing whether text present in the current screen. */ test('Timesheet Tab exists',async()=>{ await driver.elementByXPath("//*[@text='TimeSheet']").isDisplayed(); }); /** * Clicking on a component based on id. */ test('Click on button',async()=>{ await driver.sleep(3000); await driver.elementByAccessibilityId('testingIDGaveToThisButton').click(); }); /** *Sleeping for 2 seconds... *Going back * */ test('Clicking back in Timesheet dialog',async()=>{ await driver.sleep(2000); await driver.back(); }); test('Scrolling up',async()=>{ let action = new wd.TouchAction(driver); action.press({x: 250, y: 600}); action.wait({ms:2000}); action.moveTo({x: 250, y: 200}); action.release(); await action.perform(); await driver.sleep(2000); }); test('Scrolling down - 2nd time',async()=>{ let action = new wd.TouchAction(driver); action.press({x: 250, y: 200}); action.wait({ms:2000}); action.moveTo({x: 250, y: 600}); action.release(); await action.perform(); await driver.sleep(2000); await driver.back(); }); /* *Remaining Test cases will be added slowly...or you can directly add by looking at appium documentation. */`; fs.mkdirSync("./"+name); fs.writeSync(fs.openSync(babelrc, "w"), babelrcFile); fs.writeSync(fs.openSync(jshintrc,"w"),jshintrcFile); fs.writeSync(fs.openSync(jestConfig,"w"),jestconfigFile); fs.writeSync(fs.openSync(packageJSON,"w"),packageJsonFile); fs.mkdirSync("./"+name+"/__tests__"); fs.writeSync(fs.openSync(appiumTesting,"w"),appiumTestingFile); console.log("Finished"); } program .version('0.0.1') .arguments('<name>') .action(run) .parse(process.argv);