Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mtcoffee/4bc898982be62b7e56206e9a176e4e35 to your computer and use it in GitHub Desktop.

Select an option

Save mtcoffee/4bc898982be62b7e56206e9a176e4e35 to your computer and use it in GitHub Desktop.
CreateSeleniumServiceNowIncidentViaPortal
import sys
if len(sys.argv) < 4:
warning = """
Please provide 3 parameters in the format "instancename username password"
e.g. dev1234 itiluser itiluserpassword
"""
print(warning)
sys.exit()
sninstance = "https://" + sys.argv[1] + ".service-now.com"
sninstanceuser = sys.argv[2]
sninstancepwd = sys.argv[3]
#selenium setup
print('start of login')
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#chrome setup
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(options=chrome_options)
browser.get(str(sninstance) + "/login.do")
#login to ServiceNow Instnace
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "user_name")))
username = browser.find_element(By.ID, "user_name")
password = browser.find_element(By.ID, "user_password")
username.send_keys(str(sninstanceuser))
password.send_keys(str(sninstancepwd))
login_attempt = browser.find_element(By.ID, "sysverb_login")
login_attempt.click()
print('end of login')
#confirm login and move to new incident page
browser.get(str(sninstance) + "/sp?id=sc_cat_item&sys_id=3f1dd0320a0a0b99000a53f7604a2ef9")
print('current url after login is ' + browser.current_url)
expected = 'sp'
print('expected url after login is ' + expected)
if expected in browser.current_url:
print ('login success!')
else:
print ('could not access incident portal page, check login details')
exit(1)
print('end of login')
#create incident
print('logged in, now creating incident')
browser.get(str(sninstance) + "/sp?id=sc_cat_item&sys_id=3f1dd0320a0a0b99000a53f7604a2ef9")
#### DEBUG BLOCK to print all elements################################
'''
browser.switch_to.default_content()
print('XPATH ELEMENTS')
elepath = browser.find_elements(By.ID, "*")
for i in elepath:
if hasattr(i, 'id'):
print('Tag:', i.tag_name, ' ID:', i.get_attribute('id'))
'''
############################################################
#find elements and set values
browser.switch_to.default_content()
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "s2id_autogen1")))
print('setting urgency')
urgency = browser.find_element(By.ID,'s2id_autogen1')
urgency.send_keys('3',Keys.RETURN)
print('setting description')
description = browser.find_element(By.ID,"sp_formfield_comments")
description.send_keys("Testing Incident Submission with Selenium from Portal")
#submit
try:
browser.find_element(By.XPATH,'//button[text()="Submit"]').click() #submit
print('completed incident creation')
except:
print("Incident Submission failed")
#get Incident number from form
browser.switch_to.default_content()
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "data.number.name")))
number = browser.find_element(By.ID,"data.number.name")
numValue = number.text
print('Number is', numValue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment