Вышеуказанные ответы хороши для конкретного примера, который был показан, но пропустите несколько полезных случаев для более общего вопроса о естественном роде. Я только что получил один из этих случаев, поэтому создал более тщательное решение:
def natural_sort_key(string_or_number):
"""
by Scott S. Lawton 2014-12-11; public domain and/or CC0 license
handles cases where simple 'int' approach fails, e.g.
['0.501', '0.55'] floating point with different number of significant digits
[0.01, 0.1, 1] already numeric so regex and other string functions won't work (and aren't required)
['elm1', 'Elm2'] ASCII vs. letters (not case sensitive)
"""
def try_float(astring):
try:
return float(astring)
except:
return astring
if isinstance(string_or_number, basestring):
string_or_number = string_or_number.lower()
if len(re.findall('[.]\d', string_or_number)) <= 1:
# assume a floating point value, e.g. to correctly sort ['0.501', '0.55']
# '.' for decimal is locale-specific, e.g. correct for the Anglosphere and Asia but not continental Europe
return [try_float(s) for s in re.split(r'([\d.]+)', string_or_number)]
else:
# assume distinct fields, e.g. IP address, phone number with '.', etc.
# caveat: might want to first split by whitespace
# TBD: for unicode, replace isdigit with isdecimal
return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', string_or_number)]
else:
# consider: add code to recurse for lists/tuples and perhaps other iterables
return string_or_number
Тестовый код и несколько ссылок (вкл и выкл StackOverflow) находятся здесь: http: // productarchitect .com / code / better-natural-sort.py
Обратная связь приветствуется. Это не должно быть окончательным решением; просто шаг вперед.
Код, не содержащийся в блоке it, будет поднят наверх и запущен до начала любого блока it, что означает, что ваш тест, вероятно, не будет в ожидаемом состоянии в это время. Попробуйте написать свой код еще раз, но с вашим блоком подсчета внутри него.
require('..\\waitAbsent.js');
require("../node_modules/jasmine-expect/index.js");
var EC = protractor.ExpectedConditions;
describe('Demo_Test For count', function () {
beforeAll(function () {
browser.driver.manage().window().maximize();
browser.get(globalVariables.loginMain);
globalVariables.Email_Input_box.sendKeys(globalVariables.Demo_User);
globalVariables.Password_Input_Box.sendKeys(globalVariables.Demo_PWD);
globalVariables.Submit_Button.click();
browser.wait(EC.invisibilityOf(globalVariables.Submit_Button), 25000, 'submit button is not disappearing yet');
});
it('Dashboard Title Validation', function () {
expect(globalVariables.ESY_DB_Label.isDisplayed());
expect(globalVariables.ESY_DB_Label.getText()).toEqual('HomePage')
});
it("1+1", function () {
//count block
globalVariables.tableData_Dashboard.all(by.tagName("tr")).count().then(function (Count) {
console.log('\n the count of the rows are ' + Count + '\n');
});
//count block end
let i = 1;
let j = i + i;
expect(j).toBe(2);
})
});