Skip to content

Instantly share code, notes, and snippets.

@jmbauguess
Created November 23, 2015 20:21
Show Gist options
  • Save jmbauguess/704d8b07f535254b6284 to your computer and use it in GitHub Desktop.
Save jmbauguess/704d8b07f535254b6284 to your computer and use it in GitHub Desktop.
Creates page object for the LeftNav in ServiceNow
var LeftNavClassCreator = Class.create();
LeftNavClassCreator.prototype = {
HEADERS : {
'java' : 'import org.openqa.selenium.*;\nimport org.junit.*;\n'+
'import org.openqa.selenium.support.*;\nimport com.company.BaseTest;\n\n\n' +
'public class LeftNav extends BaseTest {\n',
'python' : 'class LeftNav : \n',
'ruby' : 'class LeftNav \n',
'csharp' : 'public class LeftNav \n{\n',
'javascript' : 'var LeftNav = {\n',
'protractor' : 'function LeftNavPage() {\nBasePage.call(this);\n}\n' +
'LeftNavPage.prototype = Object.create(BasePage.prototype);\n'
},
initialize: function(language) {
this.language = language;
this.data = [];
},
process: function() {
var modules = new GlideRecord('sys_app_module');
modules.addEncodedQuery('link_type!=SEPARATOR^active=true');
modules.query();
this.delegateCreation(modules);
},
writeToTable: function() {
var classGenerator = new GlideRecord('class_generator');
classGenerator.addQuery('u_tablename', 'LeftNav');
classGenerator.addQuery('u_language', this.language);
classGenerator.query();
if (!classGenerator.next()) {
classGenerator.initialize();
}
classGenerator.u_language = this.language;
classGenerator.u_class_body = this.HEADERS[this.lanaguage] + this.data;
if (this.language == 'java' || this.language == 'javascript' ||
this.language == 'csharp') {
classGenerator.u_class_body += "}";
}
classGenerator.update();
},
delegateCreation: function(modules) {
switch(this.language) {
case 'java' :
this.generateJavaVariables(modules);
break;
case 'csharp' :
this.generateCSharpVariables(modules);
break;
case 'python' :
this.generatePythonVariables(modules);
break;
case 'ruby' :
this.generateRubyVariables(modules);
break;
case 'javascript' :
this.generateJavascriptVariables(modules);
break;
case 'protractor' :
this.generateProtractorVariables(modules);
break;
}
this.writeToTable();
},
generateJavaVariables: function(modules) {
while (modules.next()) {
var table = this.replaceSpaces(modules.title +
modules.application.title + gr.order);
this.data.push(dataString = ("@FindBy(id = \"" + modules.sys_id +
"\")\n" + "public WebElement " + table + ";\n\n"));
}
},
generateCSharpVariables: function() {
while (modules.next()) {
var table = this.replaceSpaces(modules.title +
modules.application.title + gr.order);
this.data.push();
}
},
generatePythonVariables: function() {
while (modules.next()) {
var table = this.replaceSpaces(modules.title +
modules.application.title + gr.order);
this.data.push(table + " = driver.find_element_by_id('" + modules.sys_id + "')\n");
}
},
generateRubyVariables: function() {
while (modules.next()) {
var table = this.replaceSpaces(modules.title +
modules.application.title + gr.order);
this.data.push(table + " = driver.find_element(:id, '" + modules.sys_id + "')\n");
}
},
generateJavascriptVariables: function() {
while (modules.next()) {
var table = this.replaceSpaces(modules.title +
modules.application.title + gr.order);
this.data.push("var " table + " = driver.findElement(By.id('" + modules.sys_id "');\n");
}
},
generateProtractorVariables: function() {
while (modules.next()) {
var table = this.replaceSpaces(modules.title +
modules.application.title + gr.order);
this.data.push("[FindsBy(How = How.Id, Using =\"" + modules.sys_id + "\")]\n" +
"public IWebElement " + table + ";\n\n");
},
replaceSpaces: function(item) {
var regex = /\s/g;
item = item.replace(regex, '');
regex = /\W/g;
item = item.replace(regex,'');
return item;
},
'type' : 'LeftNavClassCreator'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment